Chapter 4 overviewed the new scaffolding feature included in the ASP.NET MVC 3 Tools Update. This feature makes it easy to create the controller and views to support create, read, update, and delete functionality just by setting options in the Add Controller dialog. As noted in that chapter, this scaffolding system is extensible. This section will describe a few approaches for extending the default scaffolding experience.
Customizing T4 Code Templates
The default scaffolding provided by the MVC is powered by T4 templates (T4 is a code generation engine integrated with Visual Studio). Assuming your Visual Studio installed directory was C:\Program Files (x86)\Microsoft Visual Studio 10.0\, you would find these templates in the following locations:
- C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 3\CodeTemplates\AddController
- C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 3\CodeTemplates\AddView
MVC first looks for a CodeTemplates folder in your project, so if you want to customize newly Controllers, you can copy the CodeTemplates folder directly into the root of your project and add your own T4 templates. Better yet, you can install the Mvc3CodeTemplatesCSharp NuGet package (or Mvc3CodeTemplatesVB for Visual Basic) into your project. This copies the templates into your project; it also sets the build action correctly for these files so Visual Studio doesn't try to run them ...