Create .NET Core Projects with the Command Line

Dec 10, 2020 16:57 · 2364 words · 12 minute read official exciting one looks like

Did you know that the command line is so powerful that you can actually create.NET Core projects within it? Learn more on this episode of Visual Studio Toolbox. [MUSIC]. >> Hey everyone. Welcome to Visual Studio Toolbox. I’m your host, Leslie Richardson coming to you from my very professional childhood bedroom studio. Today, I am joined by Sayed Hashimi, who is a Senior PM on the ASP.NET team. Welcome, Sayed. >> Hello. How’s it going? Definitely happy to be here.

00:34 - Been on Visual Studio Toolbox bunch of times in the past. I always love coming out here to show the latest and greatest. Thanks very much for having me. >> Yeah, welcome back. Today, we are going to be talking about creating.NET Core projects on the command line, right? >> Yeah, that’s right. What we’re planning on doing is basically creating a series of videos. The range of topics will be we’ll first start out with how do you create projects using.NET New. Then we’ll move on to using community created templates from both.NET New and Visual Studio. Then we’ll move on to then start showing how to actually create templates and how to customize this for Visual Studio. This is really just the first video in that video series. Just like what you mentioned, in this video we’ll be focusing on using.

NET New and 01:32 - understanding the basics before we get started on creating our own templates here. Yeah. >> Exciting. Why the command line? What’s the perk or why should I make a.NET Core project within the command line? >> Yeah, sure. I think that’s a great question. If we were to rewind the clock like five, six years back, before there was.NET Core. there was.NET Framework. In that world everybody was creating and building and developing inside Visual Studio itself, and I mean Visual Studio on Windows. But now, with.NET Core, we live in a different world.

02:17 - If you’re creating a project template and if you want to have the broad reach, you need to surface that in a way that can meet users where they’re at, right? Today, users that will develop with Visual Studio 2019 or maybe with Visual Studio Code or Visual Studio for Mac, there’s also third party editors out there like JetBrains Rider. You can also customize any editors like Sublime and Notepad++ to also do development with.NET Core. The way that we’ve architected this is we have what’s called the template engine. You can think about that as being like the foundation that everything would sit on. We’ve got the template engine. Then sitting directly on top of that would be. NET New command line and also, Visual Studio and Visual Studio for Mac. The idea is we can create templates using the template engine and then we can surface that in all the relevant locations. If you create a template with template engine, you can get that to appear in the.NET New command line. It can also show up in Visual Studio 2019, Visual Studio for Mac. Also, I believe Rider’s got that extensibility as well where it will show these community created templates.

03:35 - To answer the question is really you just need to meet the users where they’re at. The people developing.NET Core applications, they’re not only in Visual Studio anymore, they’re in various different places. Then also, I think one another benefit is creating a template with the template engine is very easy in comparison to the alternative technologies that are available out there. That’s why I would say that. You get the broadest reach. Then it’s also easier to create and maintain your templates if you author them with the template engine. >> Great. Yeah. I personally enjoy using the command line and I always forget that you can actually create a project in the command line to begin with.

04:18 - Can you show us how it works? >> That’s right. Yeah. Let’s let’s go check out the Terminal here. Let me get my Terminal app opened up here. I got a helicopter flying in the background Sorry guys about that. First, let’s just explore.NET New..NET New is delivered with the.NET SDK. Let’s just go ahead and execute.NET New and see what we get out of this. If you run.NET New, it’ll go ahead and show you the various different templates that are available to be used. Here we can see I’ve got a mix of different things.

04:54 - Most of these are built in templates, but we can also see I’ve got some community and custom templates installed here as well. At the bottom, there’s also some examples here, so let’s take a look at that. If I were to do.NET New, and then we would give it the name for the template that we want to create here. That’s what we see here in short name. There’s various different options here. Let’s go in and explore the MVC option here. If I was to do.NET New MVC, I could also get help that’s specific for this particular template here. You can do a.

NET New, 05:34 - template name, -h to get the template’s specific help. You can see we’ve got a bunch of different parameters here as well. The help will basically spit out the different parameters that you’ve got here. Then we can go from there. There’s also some common parameters here. Where are we at? Those are here. Some of these that we’ll be taking a look at is output. Then the install we’ll be looking at in the next video. There’s also a name value here.

06:05 - We’ll be using name and output here during this video. Let’s go and create a directory here. I’ll say demo one. Let’s go and go into that. If I just wanted to create an MVC project, I can just do.NET New MVC and then that’ll go ahead and create a project right in this particular folder here. Yes. It creates the project and then it will call NuGet to restore them. Let’s take a look at the contents here. You can see I created an MVC project and it’s named demo1.csproj.

06:42 - It got this name from the name of the folder here. If you don’t specify a name, then the folder name will be used by default. That’s the idea here. Let’s also take a look at this demo project real quick, or actually, sorry about that, let’s take a look at the program.cs. One thing that I want to point out to you. Excuse me. The project name was demo1. We can see that when I created this project, the name space was customized for that project name. Now it’s created demo1 here. >> Nice. >> Yeah.

We’ll talk a lot more 07:22 - about how these replacements work and all that stuff in our additional videos here. >> Yeah. That’s exactly what I’d expect if I were creating a template via Visual Studio. >> Yeah, that’s right. The mechanism that’s used here is the same with dotnet new as well as with Visual Studio. There’s another way that I could do that as well. Let me pick a different template here, so let’s say webapp, that’s the name of another template that we have.

07:54 - Instead of just going into the directory itself, you can specify the directory that it should be created at, and then that will also be the name of the project. I can say, dotnet new webapp MyCoolWeb. Let’s go ahead and create that. Now, that will create it into a folder that’s called MyCoolWeb. Let’s just take a look at the startup.cs for this one, I guess. We’ll see that the namespace for this one has also been set to MyCoolWeb. Now, we’ve got a project that builds and runs. I can verify that by doing dotnet build. Then we could also run this project with dotnet run or we can go ahead and load this up into Visual Studio or whatever editor or IDE that users prefer here. That was one way of doing it too.

08:53 - We also mentioned that we would show the name option there. Let me go into this new folder, demo2. If I say dotnet new webapi, and then if I give it a name, I can say MyWebApi. Now, that should create it in this folder, but with the name MyWebApi, or in the folder called MyWebApi, and then the name would be MyWebApi. If I was to inspect the program or the startup or the weather forecast, we would see that that namespace has been declared appropriately as well. Let’s take another look through the help real quick and see if there’s any additional options here that we haven’t talked about. One was dotnet new list.

09:45 - Let’s go ahead and take a look at that real quick. dotnet new -l. For dotnet new- l, this would just list out all the templates that have been installed here. Like I mentioned, I do have some custom templates installed here so if you see something different, then that means that I’ve just installed something additional that you might not have on your box. >> Yeah, I don’t recall seeing sayed tool on mine. >> Yeah, that’s right. Let me explain this output here. Here, we’ve got the name of the template. This is just a user-friendly name. Here’s the short name. That was the name that I was using before, we saw MVC, webapp, and webapi.

10:29 - Those are here, MVC webapp, and webapi is right there. Here, we can also see the language options. Let’s say if I was an F# or VB developer, I can take a look at this language column to tell me, what templates are available for that particular language as well. Then the tags are just categories. If I was interested in a console app, I could do console. Let’s do this, dotnet new -l -h. What help is available for list here? We can see that I can filter by language, and we can also filter by type here. dotnet new -l -lang F#.

11:18 - This should display only the F# templates, and then similarly for VB. I think we can also do something similar for the tags, I believe, or the type here, basically. If I was to say, –type Console. That one looks like it did enter. Oh, sorry about that, my bad. This is different. One thing that I forgot to mention here was on the list of templates that were output here, there’s a variety of different things. The vast majority of these are project templates, but we do have a few item templates here like gitignore, globaljson, so on and so forth. That’s where the type filter comes in. If I was to list the project, I can view dotnet new -l –type Project.

12:15 - This will just show me all the project templates. If I wanted to see only the item templates to replace the type of item there, you go back and see how can we filter on the tag. That might not actually be possible here to actually filter on the tags here but in Visual Studio, these tags will appear and you can actually filter in Visual Studio there. >> That is pretty sweet. I had no idea you could get all of the same functionality that we would if we were to open Visual Studio for the first time just in the command line. >> Yeah, that’s right. Then the one thing that I forgot to mention here was everything that I showed here was with built-in templates.

13:01 - But you would get the same exact functionality with custom templates as well. If I were to do dotnet new sayedweb -h, I would get the template specific help for that custom template that I’ve created here. You get the exact same experience, whether it’s a built-in template created by Microsoft or if it’s a template that you created yourself, or if a third-party company created it, you’d get the same exact experience here. It’s not like we’re special casing the Microsoft templates here. We got the same exact experience all across the board. >> That’s really cool.

Makes you feel more 13:39 - official with the templates that you create. >> Yeah, definitely. That was some feedback that we have received a lot over the course of the last several years was the template authors, they want their templates to appear in a first-class way. So when we create a dotnet new, we made sure to fulfill that promise, and we’re also trying to do the same thing in Visual Studio, and Visual Studio for Mac as well. >> That is exciting stuff. >> Yeah. I think that’s really about it here. We talked about how can we use.NET new and how can we use the help system to explore, how to learn more about the command, and also, some basics here. I think that’s really it for this video. In the next videos that follow, we’ll start seeing how can we install community templates and then use those in dotnet new as well as Visual Studio. >> That is some good stuff, I can’t wait. >> Great.

14:37 - >> Just to close out, you mentioned that you need the.NET SDK for the command line functionality. Where can users go to make sure they have that installed? >> Usually, I just go to GitHub, actually, to get that. But I think the easiest way is to just do.NET Core download, and that’ll take them to the right place here. So just dotnet.Microsoft.com to get the latest download of.NET Core. >> Can’t wait. >> Everything that I’m showing here applies to both.NET Core 3.1 as well as.NET Core 5. >> Sweet. That is very exciting. >> Thank you very much. >> Thank you. Tune in next time when we talk more about the awesomeness you can do with.NET templates and projects. Until then, happy coding. [MUSIC] .