ASP.NET Core Custom Scaffolding - Visual Studio 2019

5.8k views Asked by At

is it possible to create something like an extension for Visual Studio 2019 that adds custom scaffolding razor pages to an ASP.Net project? I want to create some templates which can be used on several projects with different class types. Is it possible to create a window like the one for adding Identity? (see below)

I found the source code of the Identity templates on Github but unfortunately I do not know how to include new templates in VS 2019 that I can pick it from the context menue.

enter image description here

3

There are 3 answers

0
Pedro Gonçalves On

Please check the section "Changing the Scaffolding" in the following link: https://learn.microsoft.com/en-us/archive/msdn-magazine/2018/march/asp-net-use-razor-to-generate-html-for-templates-in-a-single-page-app

This allow you to edit the default scaffolding templates used by Visual Studio.

Make sure that the folder you create in the project root is named "CodeTemplates". If you only want to edit scaffolding for the views you only need to copy MvcView folder to the new folder created in your project. By there you can edit the view templates for your project.

I also recommend you to install T4Editor extension in Visual Studio.

2
VRM On

The linked article solves the problem, but the article is old and needs updating.

Specifically the folder location for the templates in Visual Studio 2019 is

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\Extensions\Microsoft\Web\Mvc\Scaffolding\Templates\MvcView

The extension of the templates has also changed to "t4" instead of "tt".

Also you must put all the t4 templates in the following folder

CodeTemplates\MvcView

0
Bruno Lazzara On

I would like to provide an update for Visual Studio 2019/2022 running a project in .NET 5.0 or above:

I used the solution found in the following link: https://davecallan.com/override-the-default-controller-scaffold-templates-in-visual-studio-2019-and-net-5-0/.

I'm using Visual Studio 2022 and my MVC project is in .NET 7.0, and it works perfectly. Just make sure to have the nuget package Microsoft.VisualStudio.Web.Codegeneration.Design installed. I have version 7.0.6 of this package, and I found the templates in the folder C:\Users\USERNAME\.nuget\packages\microsoft.visualstudio.web.codegenerators.mvc\7.0.6\Templates (Note that the package name is different, but it is included with the one I mentioned).