How create and use your own ASP.NET MVC scaffolding template?
In "add Controller" wizard there is an "template" option with Microsoft templates. I would like to add new ones. A customized one. is that possible?
edit
In Visual Studio 2015 there is no template option anymore.
You can modify the T4 templates used by Visual Studio. This is a good article on how to do it:
https://www.credera.com/blog/technology-insights/microsoft-solutions/create-custom-scaffold-templates-asp-net-mvc/
You copy the default templates into your project "CodeTemplates" folder. You can then alter the templates without affecting the defaults.
Related
I want to have a Template to build my MVC project controllers.
These controllers are CRUD type. Is it possible to use custom Scaffolding?
Can I Design and Run a Template for Scaffolding myself?
After searching and searching the internet, I found and searched for Scaffolding system files, but there was no VS2019 installation folder.
How can I do this?
Just getting started with customizing the generated MVC Controller / Views.
Following Steven Sanderson's post:
http://blog.stevensanderson.com/2011/04/06/mvcscaffolding-overriding-the-t4-templates/
I used the Package Manager Console to generate 3 custom templates in my VS 2012 MVC4 project
PM>Scaffold CustomTemplate View Create
PM>Scaffold CustomTemplate View Edit
PM>Scaffold CustomTemplate View _CreateOrEdit
which creates a the files in:
CodeTemplates\Scaffolders\MvcScaffolding.RazorView
_CreateOrEdit.cs.t4 Create.cs.t4 Edit.cs.t4
But, when I "Add Controller" and choose the template
Mvc Controller with read/write actions and views using Entity Framework
The new templates are NOT being used ???
I was able to modify the create.tt edit.tt templates with notepad in
the VS 2012 folder and then generate with the changes in my project.
C:\Program Files\Microsoft Visual Studio 11.0\Common7\IDE\VWDExpress\ItemTemplates\CSharp\Web\MVC 4\CodeTemplates\AddView\CSHTML
Anyone know what I'm doing wrong ?
Thanks, LA Guy
Try creating a CodeTemplates\AddView\CSHTML folder inside your solution and put your templates there (or CodeTemplates\AddController for a controller template). It worked for me in VS2012. These can be checked into source control and everyone can share them. The ones in your VS install directory are a bit harder to share.
With the newest MVC tools, when I add a controller, I get scaffolding for the controller code as well as the CRUD Views. I'm not asking about how to override templates. I understand that I can override the T4 scaffolding templates, but I would like to see the existing built-in scaffolding templates that support the default code generation.
Where can I find the T4 code that supports the built-in/default scaffolding for MVC Views and Controllers?
Per Hansleman:
C:\Program Files (or x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp (or Visual Basic)\Web\MVC (or 2) 3\CodeTemplates
I want to create a replacement T4 template that can be called from Visual Studio's "add view". Where should I put the file? I'm not sure I should put it in
C:\Program Files\Microsoft Visual Studio 10.0\
Common7\IDE\ItemTemplates\VisualBasic\Web\
MVC 3\CodeTemplates\AddView\VBHTML
I suspect it will get wiped during repair etc if I put it there.
You can put your custom T4 template under your project like this:
{YourProjectFolder}\CodeTemplates\AddView
Of course this way it will only be enabled for this specific project, while putting them in the location you mention in your question will enable it for all your projects system wide.
Here you can find an interesting article going further into the details:
T4 Templates: A Quick-Start Guide for ASP.NET MVC Developers
There's even already a nuget package who does all the plumbing for you:
Add the ASP.NET MVC 3 Code Templates to your application with Nuget
create the following structure in the ROOT of your Project (Not solution):
CodeTemplates\AddView\VBHTML (in your case, or CSHTML for C# razor views) and copy the templates from that folder into the newly created VBHTML folder.
Also in set the Tool property in the files' Property to empty, otherwise it will ask to build the *.tt file every time you save. Also consider using Tangible T4 Editor extension for Visual Studio to edit the templates. It's available on http://visualstudiogallery.msdn.microsoft.com/
There are two possible locations. Either the one you showed which will be global for all projects on the system or in ~\CodeTemplates\AddView\VBHTML\MyTemplate.tt if you want this template to be available only for a given project. Here's a blog post about it.
I have written two separate project templates to create MVC3 projects (both based on the standard Microsoft template, but with additional controllers/views of my own) - one with ASPX views, and the other with Razor views.
But, rather than have two separate templates, I would rather mimic the way that the Microsoft MVC3 template works, which offers both view engine options, selected in a second dialog. I know that this is done with a Wizard, but I can't find the Wizard in the MVC source code (which I've downloaded). Does anybody know where to find the Wizard, or have other helpful suggestions? Thanks.
The custom MVC New Project dialog exists in Microsoft.VisualStudio.Web.Mvc.3.0.dll. We do not ship the source code for that assembly.
I used the Export Template Wizard to create an ASP.NET MVC3 Project Template of my own. This worked OK, but did not offer the same options that Microsoft ships with their Visual Studio MVC assembly. I also ran into an issue where the strongly-typed views were not being updated with the correct namespace. They were keeping the namespace of my original project, while the Controllers and Model was correct.
We would love to have the source code made available for MVC 3 Project Templates!
Here is the Wizard I used. It definitely comes in handy. http://visualstudiogallery.msdn.microsoft.com/57320b20-34a2-42e4-b97e-e615c71aca24/
Phil Haack has documented how to create a custom MVC3 project template that will appear inside that wizard (as opposed to a normal project template that won't)
haacked.com/archive/2011/06/06/creating-a-custom-asp-net-mvc-project-template.aspx
If you follow that process you'll only end up with razor as the possible view engine. To get other view engines, you need to expand the reg files to list spark etc. Details on doing that are provided in this stack overflow answer:
Add custom viewengine to New project dialog?