Add the ASP.NET MVC 3 Code Templates to your application with Nuget

Before (Manual)

ASP.NET MVC uses T4 templates to scaffold Views and Controllers through the Add View and Add Controller dialogs. A lot of people probably don’t know that these templates can be customized quite easily to generate whatever markup you prefer. Really, the only pain point is finding the code templates on your machine and copying them with the expected folder structure into your project. Unfortunately, this can be a bit of a hassle because ASP.NET MVC uses a custom T4 host to scaffold views and controllers but Visual Studio associates the files with the built-in T4 host. This means that the built in T4 engine will run for each .tt file that you add to your project. It will also run again when you save the .tt files. You can manually clear this custom tool and delete all of the aspx/cshtml files the built-in host generates and begin customizing the templates.


After (With Nuget)

The Nuget packages, Mvc3CodeTemplatesCSharp and Mvc3CodeTemplatesVB, will add all of the view and controller templates that ship with ASP.NET MVC 3 to your application. Also, the DTE will clear the custom tool on each template that is added to your project so you can begin editing the files immediately. The one gotcha is that the built-in t4 host does run once before the DTE can clear the custom tool so you will still see one spurious T4 related error in your error list until you re-open the project. Make sure to install the package from the command line since the DTE needs to run.


After installing the package you should see something similar to the screenshot below:


You can see all of the logic used by ASP.NET MVC 3 to scaffold views and modify it as you please. Then just run the Add-View tool to add a view with the new markup. If you don’t want these templates to override the default templates installed on your machine you will need to rename or delete the templates in your project that you do not want to take precedence. I’ll post more on this in the coming weeks.

Hope this helps you get started editing the ASP.NET MVC 3 Code Templates!

Joe Cartano | ASP.NET

Comments (4)

  1. Bob says:

    Nuget seems to be providing an unnecessary layer of overhead that provides no significant benefit.  A project template can be built to do all this.  Why add the annoyance of such an irritating and unnecessary tool?

  2. joecar1 says:

    For me, rather than have a bunch of different project templates I would rather start from one place and pull in what I want to use in my project. I don't really see much overhead because I can just grab whatever I want with one command.

  3. jeroenk-tritac says:

    Note that the folder names are case sensitive, it must be 'AspxCSharp'.

  4. Thanks says:

    This is great work, thanks. Could have saved myself loads of time if I'd bothered researching it earlier…

Skip to main content