Chapter 4 overviewed the MVC 4 use of scaffolded views, which make 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 Chapter 4, this scaffolding system is extensible. This section describes a few approaches for extending the default scaffolding experience.
Customizing T4 Code Templates
The default scaffolding provided by 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 11.0\, you would find these templates in the following locations:
- C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 4\CodeTemplates\AddController
- C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 4\CodeTemplates\AddView
MVC first looks for a CodeTemplates folder in your project, so if you want to customize new controllers, you can copy the CodeTemplates folder directly into the root of your project and add your own T4 templates.
Visual Studio will complain with the following message:
Compiling transformation: The type or namespace name ‘MvcTextTemplateHost’ could not be found (are you missing a using directive or an assembly reference?)
The reason for this is that when adding a T4 file to a project, Visual Studio sets the value of the Custom Tool property ...