Modifying or Adding the Default Code Generation/Scaffolding Templates in ASP.NET MVC3 Using T4 Code Generation


Description

Well T4 (Text Template Transformation Toolkit) Code Generation is for Visual Studio 2008 and above.

If you're doing something twice or more, manually, in your company, generate it.

We have noticed in our ASP.Net MVC3 applications that when we add a new controller class or strongly typed view and choosing our required scaffold column,
automatic code is generated in the controller class or view engine.

Now we can modify the scaffolding template code or we can write our own code for generating the scaffolding template.

We can also create our own scaffold template for the controller and view class.

Actually when we use the Visual Studio "tooling" (that means dialogs and stuff) to Add View or Add Controller, we are actually executing a T4 template and generating a little bit of code.

Now the big question is: where does this start and how can we change it?

For that first you have to first reach in your Windows system by the following location:

C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 3

T4MVC1.gif

Now explore the "code template" folder. You will have two folders:

  1. Add controller
  2. Add view

Now explore the "Addcontroller" folder. You will see 2 files with ".tt" extension like in the following picture:

T4MVC2.gif

Now in ASP.Net MVC3 when we add a new controller it will ask the template with the 3 options like the following picture:

T4MVC3.gif

These code generated template options are those 2 files. We can modify the existing one or create a new ".tt" file that will come under the "scaffold options template".

But if we modify them here, the templates will be effected globally. That means that in every ASP.Net MVC3 project we will get our modified or newly added templates.

So for the custom template generation for just one specific project, we have to do the following steps:

Step 1:

Copy that folder (or just subfolders like AddController, or AddView) to our ASP.NET project.

We can do this by dragging from the Explorer directly into the Visual Studio Solution Explorer as seen here:

T4MVC4.gif

Now select the Templates using Ctrl-Click, then right-click and select Properties. (You can delete any templates you won't be using.)

T4MVC5.gif

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 or controller you can make your own.

Now open the "controller.tt" file. In that file I am going to write my own controller event, say:

public ActionResult DetailsCaragory(int id)
{
return View();
}

After that I save that file.

Now I am going to add a new controller class.

I have choosen the template with "Controller with empty read/write actions". And press the "add" button.

T4MVC6.gif

It will generate the controller class with my custom code like the following picture.

T4MVC7.gif

We can also create or own view code generated template or modify an existing one.

Actually this technique is helpfull when the project you are using has lots of repeated code for crud operations.

Conclusion

So in this article we have seen how to modify the existing code or adding our own custom code generation for scaffolding column for generating
a Controller class and view in ASP.Net MVC3 using T4 Code Generation.