3
votes

I'm trying to figure out how (or even if) I can create a custom Visual Studio project template that hooks into the existing ASP.NET Core Web Application template available in Visual Studio 2019?

What I want to do is something similar to madskristensens ASP.NET Core Template Pack (GitHub source code). However instead of VS 2017 I want to do this for VS 2019's revamped "New Project Dialog" window. So imagine adding an additional ASP.NET Core web application template in the place highlighted below.

New Web Application Dialog

I am able to create new project templates (both for Visual Studio and for dotnet new) but nothing has worked thus far. Has anyone been able to extend VS 2019 in this way, or was this taken away after VS 2017?

1

1 Answers

7
votes

After doing some more investigation between both VS 2017 and 2019 (Community) I was able to successfully figure this out! Man oh man do we need some docs on this because this felt like a doozy!

I was able to use the ASP.NET Core Template pack as a building block for putting something together. For those who don't know, the extensions are designed to add templates to Visual Studio using .nupkg file(s) embedded in the extension. The use of the .nupkg files is similar to how dotnet new works with custom templates. I've got a working prototype on GitHub that supports both Visual Studio 2017 and 2019.

Now if you're like me, you may look at either madskristensens or my project and ask "How in the heck does this work!?" Great question! Here are the details I have the time to fill out right now:

  1. Build out a custom template (or templates) and place them into *.nupkg file(s)
    • In my sample, my SampleTemplates project contains three different project templates. I generate the .nupkg with dotnet pack
  2. A vs-2017.3.host.json file is required in the .template.config/ folder so that the ASP.NET Web Application wizard can display the template.
    • At least 1 or more other requirements need to be fulfilled to be displayed in the ASP.NET Web Application wizard, but I haven't yet figured those out, as adding the file to a console app template doesn't cause it to appear. I want to try and figure this out, although I'd love help if anyone already knows!
  3. The template.json needs a Framework symbol to define the list of .NET Core framework targets that are supported by the template.
    • In doing some testing, it didn't look like the Framework symbol didn't substitute into the .csproj, so that is why my example also includes a TargetFrameworkOverride symbol that the Framework symbol replaces.
  4. Place the .nupkg file(s) into the root of a VSIX extension project and make sure to set the "Include in VSIX" flag to True. I emphasize root because originally I had the NuGet package landing in a build\ folder in my extension and my templates weren't being picked up.

I'll try to put together a README in my example project to provide a better outline of all of the details required to accomplish this. Fingers crossed this helps someone out (or at least help me out in the future when I inevitably forget how I accomplished this)!

Update

As of September 2nd, 2020, the Visual Studio team released an experimental feature to include dotnet new templates within Visual Studio. I have done some exceptionally minimal testing with it, and it does appear to work, but it's not perfect. For example, I created a dotnet new template that scaffolds multiple .csproj files and .sln file, and the output didn't quite match my solution.

This is the Visual Studio blog post describing the announcement: https://devblogs.microsoft.com/dotnet/net-cli-templates-in-visual-studio/