21

Now that I figured out how to enable scaffolding in ASP.NET Core MVC (see View scaffold templates in ASP.NET Core), I'd like to create custom template files for Views. I found one place that said the template files are located here:

C:\Users\{user name}\.dnx\packages\Microsoft.Extensions.CodeGenerators.Mvc\1.0.0-rc1-final\Templates\ViewGenerator

But I copied an existing file there and that copied file does not appear in the Template dropdown on the Add View dialog. Are they located elsewhere, or do I have to do something to get files added in that folder to appear? Restarting Visual Studio did not do that.

Community
  • 1
  • 1
Rono
  • 3,171
  • 2
  • 33
  • 58

4 Answers4

35

I am using Microsoft.VisualStudio.Web.CodeGeneration.Tools 1.0.0-preview2-final

Templates are located here...

C:\Users\{username}\.nuget\packages\Microsoft.VisualStudio.Web.CodeGenerators.Mvc\1.0.0-preview2-final\Templates

Copy the Templates folder to your project and edit them as required.

enter image description here

user3012633
  • 464
  • 5
  • 6
  • 2
    It would nice for this to be global, so I don't have to copy these around to different projects. Also, you can edit an exiting template file, but when I added a new one, it didn't appear. This isn't ideal, but it works. – Rono Sep 19 '16 at 18:00
  • 2
    @Rono - I customise my templates, then package for NuGet using NuGet package explorer. You can then either publish this package to a private NuGet server, or to a folder and add the path to your NuGet sources in Visual Studio. Easy to reuse, update, and keep versioned. – dmcquiggin Nov 02 '16 at 11:51
  • @dmcquiggin - if you spell that out in an answer, I may switch acceptance to yours. – Rono Nov 02 '16 at 12:38
  • This is great and saved me time trying to figure it out. How would one create a new View for the ViewGenerator? I tried creating a new cshtml file but it wasn't being generated. Is there a config file somewhere that needs to be updated as well? – Lance Jun 05 '17 at 20:34
  • @dmcquiggin Is there a way to add new templates without publishing it as a nuget package? – Rik van den Berg Jul 23 '17 at 08:46
  • @RikvandenBerg Yes - in fact this answer actually shows you exactly how to do that; once you have copied them from the NuGet package into your project, just edit them / add new templates in your project, following the same folder structure. – dmcquiggin Jul 23 '17 at 16:20
  • 1
    @dmcquiggin I would assume as well, but it seems the templating doesn't detect any of the new templates I've made. Editing existing templates work but when adding new templates, it doesn't show up in the scaffolding menu. Am I missing anything? – Rik van den Berg Jul 23 '17 at 19:44
  • 1
    Same issue - can edit existing templates, cant see any new ones in the controller scaffold menu list. Did anyone figure this out? – statler Sep 13 '17 at 22:50
  • I'm also seeing the same issue, I would expect there should be an extra step to allow VS to know that the new Templates folder is where it should look to find template files? – defect833 Sep 20 '17 at 01:37
  • The path listed here (yes I changed to my username) doesn't exist, I cant see the Microsoft.VisualStudio... folder. How do you do this in 2018 with .net core 2? – niico Apr 26 '18 at 13:12
  • I am following this, I need to get model_primary_key_field_name https://stackoverflow.com/questions/72599587/asp-net-core-find-model-primary-key-field-name-in-custom-scaffolding – alamnaryab Jun 13 '22 at 08:13
22

I´m using Visual Studio 2017 and asp.net Core 2.0, and after search a lot, I´ve just found the Templates on this place: C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.visualstudio.web.codegenerators.mvc\2.0.0\Templates

I hope this could help someone with the same issue... ;)

Fabio Dalonso
  • 311
  • 3
  • 2
5

Latest version as of Oct 2018 could be found in GitHub of the Asp.NET https://github.com/aspnet/Scaffolding/tree/master/src/VS.Web.CG.Mvc/Templates

codevision
  • 5,165
  • 38
  • 50
  • Also, this was a big GOTCHA for me: If you are using Bootstrap3, the expected directory for custom view templates within the project is Templates\ViewGenerator_Versioned\Bootstrap3\ – davrob01 May 21 '20 at 22:36
-3

You can enable code generation rather than copying from templates folder.

Using the tutorial Asp.Net Core Using existing db

Tools > Nuget Package Manager > Packagae Manager Console

Run the following commands one after the other. Make sure you have internet connection.

Install-Package Microsoft.EntityFrameworkCore.SqlServer
Install-Package Microsoft.EntityFrameworkCore.Tools
Install-Package Microsoft.VisualStudio.Web.CodeGeneration.Design

Rebuild the solution. Then right click on the controller folder > new > controller...

Israelo
  • 5
  • 2
  • 2
    The question is asking how to edit the scaffolding templates whereas your answer is explaining how to scaffold a new item. – jmdon Apr 24 '18 at 11:20