Use an Existing .NET Core Project Template

Dec 10, 2020 16:58 · 1721 words · 9 minute read changing making bots actually execute

You’ve got your default templates, but there’s also custom templates that so many people within the.NET community have created that you can take advantage of. If you want to learn how, find out on this episode of Visual Studio Toolbox. [MUSIC] >> Hey everyone, welcome to Visual Studio Toolbox. I’m your host, Leslie Richardson and I’m joined once again by a Senior PM Sayed Hashimi. Welcome.

00:25 - >> Hello, how’s it going? Thank you for having me back. >> Yeah, likewise. Once again, we’re going to be talking about Project Templates and specifically how to take advantage of the Project Templates that the.NET community creates. Can you tell us more about that? >> Yeah, that’s right. Some functionality that we have built in to dotnet new and also now Visual Studio, and Visual Studio for Mac is being able to install custom templates that have been created by third parties; templates that have been created by the community. Some of them might be open-source templates, others might be created by companies like Amazon or various different kind of companies there.

01:07 - Yeah, it’s all about how to find templates, how to install them, and also how to use them from dotnet new and Visual Studio. >> That’s really cool. I’m sure there’s tons of different reasons why you’d want to take advantage of the different templates that companies and just random people create. I’d love to see more. >> Yeah, that’s right. Great. When we’re working with community templates or custom templates here, the first kind of hurdle is, how do you discover these templates. Let me go into my browser here and then I’ll show you a few different options for how you can discover templates. Your first option is NuGet.org. Here’s where I can search for templates. Let me show you how to do that. There’s a popular template that’s called.NET boxed. I can do a search on NuGet.org and then in the filter here, I can expand the filter and then select “Template.

” 02:07 - Now, this will only show me dotnet new templates. If I was to drill into this Boxed.Templates here, instead of the command to add a package to a project, we can see that you get the dotnet new install command here. I can just go ahead and copy this command, and then drop into a terminal and then just paste that. Then this will install that template. Then we can use this from the command line, as well as in Visual Studio and Visual Studio for Mac. I just installed that one. Let me see where. It’s here. Here’s the boxed templates. Yes, we got the ASP NET Core API boxed and the graph one is from there, the Orleans one and the NuGet package.

02:54 - That’s one option for how to discover community templates. Another option is, in my spare time, I’ve developed this website dotnew.azurewebsites.net. You can go here to also find community templates. On NuGet.org, it’s just searching through the metadata for the package itself, but on this website, it’ll actually look for the would-do searches for the individual templates as well. Let’s say, if I wanted to find templates that were created by Mads Kristensen, I can do a search for Mads.

03:31 - We’ll see there’s several different kinds of templates here that Mads Kristensen has created here. We can save and gets his mini blog templates. I can go in and drill into that as well. Here, you can see we get that same exact command for how to install the template. Then in addition, there’s also command for how to actually execute this particular template here. That’s another option for how to find templates.

03:58 - They will be adding functionality into dotnet new to search and find templates as well, but that experience is going to be changing. I’m not going to go through what we have today, because it’s going to be significantly different. Now, let me go back to NuGet.org. If was to search for Sayed, I’ve got a template here that I’ve already created. This template is available on GitHub as well. Let me go ahead and copy the command to get that one installed. This template is available on GitHub and you can get to it through this aka link, aka.ms/netcore-templates. That will bring you to this GitHub repository. Let me go ahead and install this template because this is the one that we’re going be working throughout this set of videos here. So let me get this one installed here. Now, that I’ve installed that, we can see I’ve got some additional templates. I get Sayedtool, Sayedweb, and Sayedwebauth.

05:02 - Now, what I need to do is let me go into Visual Studio and I will show you what needs to be done in order to get these templates to show up. The first thing that you’ll need to do is to go and turn on this preview feature to show these templates. I went to Tools Options, then I’m going to go down to Preview Features and then we will scroll until we get to show all.NET core templates. You check that checkbox and then you’ll have to restart your Visual Studio to get that option to appear. I’m going to go ahead and restart Visual Studio here as well since I installed that additional template.

05:41 - Now, all I did was, I ran the dotnet new install command a couple of times to get these templates installed with dotnet new. Then after I check that checkbox, now we should see the same exact templates starting to appear in Visual Studio itself. For example, if I was to search for box, I should see the ASP.NET boxed templates. I have another community template pack install, it’s a template for making bots, essentially, so that we can use those as well, but let’s go back to the one that I’ve created here. We can see now, I’ve got those three templates that were installed on the command line.

06:22 - I’ve got my Sayedtool, which is a console application and then I’ve got two different kinds of flavors of the web app here. Let me go in to just a regular vanilla web app here and let’s take a look at the experience. But let’s also do note that we do have the same exact experience in Visual Studio for this custom template as what we have for the templates that are built-in, right? So I can customize the icon, I can customize the name and description as well as these tags. If I was to search on these tags, the search results would include these custom templates as well. For all intents purposes it looks like this is a built-in template. You can’t tell the difference between it. >> That’s really cool.

07:06 - >> Yeah, let’s go to next here and we can see I get the same exact experience as they would for a built-in template here. Now, I’ve got this customized name as well. Let’s say my new web. Go next We can see we also have some custom parameters here. I’ve got the target framework like what I would expect, but I also have parameters here that are specific to this exact template, right? >> Right. >> We can say, Sayed I Hashimi. For a title, we’ll say demo. >> I think that has to be the author name. >> Then create that one.

Then I’ll show 07:46 - you some of the things that have happened here after we created this project. We created the project. Let me go and take a look at the program.cs. We can see, when I created this project, I gave it a name of my new web. As expected, the namespace is set to my new web and that’s just how the other templates behave. Let me go into the index.cshtml, I can see that my author name has been placed here. Then the site title is in the layout here somewhere, Demo, yeah. There it is. Demo, yeah.

08:27 - Let me just do a Control Shift B to go ahead and build this guy. We can see here that the project was built successfully. I can obviously run this with Control F5 or F5. I think the point that I want to really stress here is, you create a template onetime, you install it, and users will get the same exact experience for the built-in templates, as well as these third party templates. What I’ve shown here is inside Visual Studio, but we also have the similar support in Visual Studio from AX.

09:03 - They also have a preview flag that you need to go and enable. There’s details here at that aka link that I showed earlier, it was aka.ms/netcore-templates. If you were to scroll down here, you’ll see, how do I enable it in Visual Studio and then how do I enable it in Visual Studio for Mac. That’s the options there and that’s really about it for this video here. We covered how to install a template. We covered how to use that template from the dotnet new console.

09:41 - I’m not sure if I actually exercise that here, but it’s the exact same way that you do it with a built-in template and we covered that in the previous video and we also showed how to use it in Visual Studio 2019. >> That is awesome. I mean, I just love how those custom templates are made to feel like not inferior to the built-in ones because for many people, I’m sure those custom templates are essential for their development. >> Yes, absolutely for sure. >> As we mentioned before, this is part of a larger series on.NET templates. What are we going to be talking about next time? >> I think next time we’re going to be getting into how to actually create your own template. I think we’ll be focused on creating a template and then using that with dotnet new.

10:29 - Then after that, we’ll get into how do we fine tune that for Visual Studio. Yeah. >> Good stuff. Tune in next time. Until then, happy coding. [MUSIC] .