Scott Hanselman

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

March 28, '11 Comments [13] Posted in ASP.NET | ASP.NET MVC
Sponsored By
image

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:

image

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.)

image

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 List.tt" as a copy of List.tt and it shows up in the Add View dialog.

image

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
Sponsored By
Hosting By
Dedicated Windows Server Hosting by ORCS Web
Monday, March 28, 2011 11:47:20 PM UTC
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 controller.tt so that is does that by default. Good stuff.
Tuesday, March 29, 2011 12:21:13 AM UTC
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)

http://nuget.org/List/Packages/Mvc3CodeTemplatesCSharp
Tuesday, March 29, 2011 1:17:34 AM UTC
Greg - Nice! I was debating if I should make one.
Tuesday, March 29, 2011 6:13:27 AM UTC
Awesome! Thanx
Tuesday, March 29, 2011 10:35:46 AM UTC
Thank you Mr. Hanselman :D
Kim Tranjan
Tuesday, March 29, 2011 11:33:35 AM UTC
very handy indeed great reminder as I had forgotten about this
Wednesday, March 30, 2011 3:14:38 PM UTC
Ahh the sweater is kickin man!! Great content though :)
Monday, April 04, 2011 10:52:39 PM UTC
but, the sweater is really nice ! :)
Thursday, May 12, 2011 10:37:21 AM UTC
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 http://stackoverflow.com/questions/5938872/where-can-i-find-the-api-specification-for-mvctexttemplatehost

Thanks!
Sergi
Thursday, May 12, 2011 11:20:31 PM UTC
Sergi, I answered your StackOverflow question. Hope it helps.
Friday, May 13, 2011 6:34:58 AM UTC
Great! Thanks for your time. I'll post there.
Friday, May 27, 2011 8:10:31 AM UTC
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?

Thanks
Hemant
Wednesday, November 02, 2011 7:17:03 PM UTC
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.