ASP.NET Core Custom Scaffolding - Visual Studio 2019

Viewed 4727

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

2 Answers

Please check the section "Changing the Scaffolding" in the following link: https://docs.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.

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

Related