Scott Hanselman

Modifying the default code generation/scaffolding templates in ASP.NET MVC

March 29, 2011 Comment on this post [13] Posted in ASP.NET | ASP.NET MVC
Sponsored By

One of the things people like the most about my ASP.NE MVC 2 Ninja Black Belt Tips video, besides the Bill Cosby sweater, is the tip where I show how to modify the default CodeTemplates that are used in Code Generation in ASP.NET MVC (either version).

Eilon mentioned it on an internal mailing list this week so I thought it'd be worth surfacing again in case you haven't heard of this, Dear Reader.

Soon, we'll see even more powerful, flexible, and fun ways to customize your own Scaffolding in ASP.NET MVC 3, so keep an eye out, 'cause it's coming.

Bring the CodeTemplates local to your project

imageWhen you use the Visual Studio "tooling" (that means dialogs and stuff) to Add View or Add Controller, you're actually executing a T4 template and generating a little bit of code. Where does this start and how can you change it?

Try this. Create a new ASP.NET MVC project.

Now, go to:

C:\Program Files (or x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp (or Visual Basic)\Web\MVC (or 2) 3\CodeTemplates

See all those folders? Turns out everything under CodeTemplates are T4 templates that can be either modified in place or even better, can be brought local to your project. Local CodeTemplates will override the global ones.

Copy that folder (or just subfolders like AddController, or AddView) to your ASP.NET project. I do this by dragging from Explorer directly into the Visual Studio Solution Explorer as seen  here:


You might get some errors when you drop the folder. Ignore them and instead, select the Templates using Ctrl-Click, then right click and select Properties. (You can delete any templates you won't be using.)


Now, see where it says Custom Tool? Clear that string out completely. You're tell Visual Studio that you don't want these T4 Templates to be run during a build. They will only be called manually by the Tooling dialogs like Add View and Add Controller.

You change these templates, or in the case of Add View you can make your own. For example here I've made a new "My Awesome" as a copy of and it shows up in the Add View dialog.


Now I can not only totally customize what gets generated from the Add Controller and Add View dialogs, I can add custom templates to the drop down. Most importantly, these changes live in the CodeTemplates folder of my project and are carried along with my project in Source Control so others on my team or company can use them as well.

Have you customized yours? Enjoy.

Related Links

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
March 29, 2011 3:47
I really like how it can be project specific and stored in source control. I can see overriding the default list so that it renders something like a telerik MVC grid, for example. Also, since all of our contorllers inherit from a base controller we could modify so that is does that by default. Good stuff.
March 29, 2011 4:21
There is a nuget package that adds the mvc3 codetemplates folder to your local project. Could take some of the pain out of locating the files. (Not that its THAT painful)
March 29, 2011 5:17
Greg - Nice! I was debating if I should make one.
March 29, 2011 10:13
Awesome! Thanx
March 29, 2011 14:35
Thank you Mr. Hanselman :D
March 29, 2011 15:33
very handy indeed great reminder as I had forgotten about this
March 30, 2011 19:14
Ahh the sweater is kickin man!! Great content though :)
April 05, 2011 2:52
but, the sweater is really nice ! :)
May 12, 2011 14:37
Hi Scott,

I've been working with T4 for a long time and I am a great fan of it; and it's great to see that this templates for MVC are fully and easily customizable.

I am trying to develop my controller template, and I was wondering where/how to find and API specification of the MvcTextTemplateHost, and how it interacts with the UI Dialog "Add controller...", and if it's possible to customize/develop our own dialog that runs these templates. The same applies for the "Add view..." templates and dialog.

I left my question at

May 13, 2011 3:20
Sergi, I answered your StackOverflow question. Hope it helps.
May 13, 2011 10:34
Great! Thanks for your time. I'll post there.
May 27, 2011 12:10
Hi Scott,

I have added few more view templates via MVC scaffolding. The folder structure mentioned here and the one, scaffolding follows are different. Is it possible to use the Scaffolding structure to add the names of custom views in Add View dialog? Or do I need to duplicate the files and keep synching them?

November 02, 2011 23:17
That was really helpful for my current MVC3 project! Custom templates will save whole bunch of time in future!
Thanks Scott!

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.