.NET CLI Templates in Visual Studio
One of the values of using tools for development is the productivity they provide in helping start projects, bootstrapping dependencies, etc. One way that we’ve seen developers and companies deliver these bootstrapping efforts is via templates. Templates serve as a useful tool to start projects and add items to existing projects for .NET developers.
Visual Studio has had templates for a long time and .NET Core’s command-line interface (CLI) has also had the ability to install templates and use them via `dotnet new` commands. However, if you were an author of a template and wanted to have it available in the CLI as well as Visual Studio you had to do extra work to enable the set of manifest files and installers to make them visible in both places. We’ve seen template authors navigate to ensuring one works better and that sometimes leaves the other without visibility. We wanted to change that.
Starting in Visual Studio 16.8 Preview 2 we’ve enabled a preview feature that you can turn on that enables all templates that are installed via CLI to now show as options in Visual Studio as well. To enable this option visit the Preview Features options in the Tools…Options menu and look for the “Show all .NET Core templates in the New Project dialog” (we’re awesome at naming) checkbox and check it:
After enabling you need to restart the Visual Studio instance to get this capability. After restarting and choosing to create a new project you’ll see some slight differences in the experience. The full list of templates is shown and the names are now being driven from the manifest data in the template’s template.json file.
Previously as an example we had a special dialog for ASP.NET projects. When the new experience is enabled, this no longer exists, and all project templates use the same infrastructure. This new model reads the options to be exposed to the dialog and renders the UI to enable the selection. Here is the example of an ASP.NET Core web application:
We know the .NET ecosystem has a lot of commercial and community projects that create templates and have instructions for developers to install them from their NuGet packages. We want to surface those better and provide more attribution to the authors when they are. For example, .NET Boxed templates provide a set of starter templates but are installed only via CLI. Here you see them now showing in Visual Studio with author attribution:
We’ve just enabled this capability in this release and are refining it based on our own findings but also hopefully from feedback from template authors. If you are a template author please turn this on and try it out! Try enhancing template.json in your CLI template to offer the Target Framework selection and let us know how your template options look in the new dialog. This is one step in our journey to a better template ecosystem; let us know how we are doing and what other changes you’d like. We’ve already identified a few tips to make your templates a great experience:
- Make sure sourceName is in template.json (should be required to show in Visual Studio), here is an example
- Add a defaultName to the template.json (defaultName is the value that is displayed for the project name in VS, for example WebApplication1), here is an example
- Add an icon via adding a host file, here is an example
As we evolve this integration we will continue to provide guidance and update our templating documentation to reflect the best practices for authors to deliver a great experience in CLI and Visual Studio before we turn this on by default.
There are a few other experience things to note when enabling this option:
- For now, this support is for Project Templates, but we are exploring the options to enable this for Item Templates as well.
- Not all the Authentication options are enabled for ASP.NET Core projects but will be added later.
- Templates with CLI and Visual Studio installer (VSIX) manifests with different IDs will show duplicates. Changing to a single identity for the template is recommended.
Please give this a try and use the Visual Studio Feedback Tool to report issues you may find in your experiences. If you can provide the specific template you are using that will be useful as well.
On behalf of the Visual Studio and .NET teams, thanks for reading and happy coding!