ASP.NET MVC 1.0 RC1 and T4 Templates – Add Controller…

The news that ASP.NET MVC now has some integration with T4 Templates got me a bit excited thinking about the code generation capabilities.


As Scott pointed out in his post, there is now an “Add Controller…” option:


 



 


that will pull a template, called Controller.tt, from within:


Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC\CodeTemplates\AddController


and generate a controller for you based on that template.


The cool thing is that you can customize that template based on your desire to generate more than just the stub of a controller that it does out of the box. You can add all kinds of code in there based on your coding conventions that includes validation, logging, databinding, etc. This is extremely cool and I have been creating a custom Controller T4 Template that embodies my development style.


There are a couple of limitations that I have noticed, however:




  1. Only 1 Controller Template – Unless I am missing something, you can’t choose a particular controller template like you can with a view template. This isn’t a deal breaker and maybe not even a big deal when I actually start using the template in development, but it seems like it would have been nice to be able to choose the controller template from a list of available templates in the folder just like one can with a view.


  2. Only works with default Controllers Folder – It appears that the “Add Controller…” option only works when you are on the Controllers Folder that is generated by the MVC Project Wizard. I have a custom project that uses “Areas” and the “Add Controller…” option will not display when I choose my own custom folders for adding a controller.

 


If any of the above is possible, please let me know.


In general, however, I think the addition of using T4 Templates in the ASP.NET MVC Framework to generate controllers and views was a brilliant idea. Kudos to the MVC Team for adding it.


 


David Hayden


 

This entry was posted in Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

5 Responses to ASP.NET MVC 1.0 RC1 and T4 Templates – Add Controller…

  1. Sound great John!

    Thanks for the heads up (hehehe a RTFM moment :))!

  2. David Hayden says:

    Thanks for pointing that out, John!

    That is perfect for when we want templates per project.

  3. John Meyer says:

    Dave,

    Check out the read me file for the MVC RC (page 8), it explains how to override default behavior of those menu items and how to have a project specific templates folder

  4. David Hayden says:

    Good point. I like that idea.

  5. Well generally, bigger projects often have customized templates just for them (with custom base classes, initial actions and such). It would be great if we could get for example a convention on templates folder in the *solution* directory or something, having templates there would override the location of Visual Studio.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>