pk software dev blog

custom microsoft .net application development

16. October 2008 01:44
by Paul

ASP.NET MVC Generator - First Cut

16. October 2008 01:44 by Paul

[Update – due to the very nice T4 support and addins the MVC team have enabled (see for example) this side project has been shelved]

I’ll keep this brief, this is the kind of thing you are better off trying for yourself.

Rails and MVC Stuff

One of the things the rails guys enjoy with developing their sites is basic code generation (skeleton files) for models, controllers and views (and whatever else). This "ASP.NET MVC Generator" is a C# implementation of the same idea making use of the standard ASP.NET MVC templates that come with the framework (currently preview 5 the beta).

(Note that for now there is a minor issue around the VB generation because of the lack or namespaces etc...)


I implemented controller and view generators with a hybrid of the two I call a "controller set". The generator I like the best is the "Controller Set". This generator creates the controller with its actions and the relevant views for each action in the appropriate view area. For example, I have an MVC website (called mvc1) setup in "C:\DEV\MvcSample\". I copy the generator and its templates to the root of the project. I have not worried so much about a Model generator yet because most of us will probably go for LINQ to SQL or SubSonic etc. I plan on adding the Model soon (not that it's hard!) but it's geared more towards the NHibernate/Active Record style of persistence.

Directory of C:\DEV\MvcSample
15/10/2008  01:50 PM            26,112 AspNetMvcGenerator.exe
15/10/2008  04:42 PM    <DIR>          mvc1
15/10/2008  04:39 PM    <DIR>          MvcTemplates 

I type the following command, sets the root namespace of the code generator to "MVC1" and the base directory to "mvc1" (where the website is from this dir) and the writes out a settings file (called "mvcsettings.xml"). The settings file is loaded if present by default to save typing the namespace etc every time.

C:\DEV\MvcSample>AspNetMvcGenerator.exe /ns:MVC1 /basedir:mvc1 /ws
Directory of C:\DEV\MvcSample
15/10/2008  01:50 PM            26,112 AspNetMvcGenerator.exe
15/10/2008  04:42 PM    <DIR>          mvc1
15/10/2008  04:44 PM               631 mvcsettings.xml
15/10/2008  04:39 PM    <DIR>          MvcTemplates 

Let's check the contents of the settings file:

C:\DEV\MvcSample>type mvcsettings.xml

<?xml version="1.0" encoding="utf-8"?>

<SiteProperties xmlns:xsi="" xmlns:xsd=""> 


  <Area />


  <ContentTags />



  <SafeItemName />

  <ActionMethods />







These are the parameters that are used for substitution in the templates. You can make other changes etc if for example your views or controllers do not reside in the default locations.

For this example I am going to delete the Home controller and its view area (actually one thing I have not got around to doing yet is checking for existing files etc.) The solution looks like this before the code generation:


Boring! Now for the good stuff. Type the following command:

C:\DEV\MvcSample>AspNetMvcGenerator ControllerSet Home Index AboutUs Contact

Wrote: C:\DEV\MvcSample\mvc1\Controllers\HomeController.cs

Wrote: C:\DEV\MvcSample\mvc1\Views\Home\Index.aspx

Wrote: C:\DEV\MvcSample\mvc1\Views\Home\Index.aspx.cs

Wrote: C:\DEV\MvcSample\mvc1\Views\Home\Index.aspx.designer.cs

Wrote: C:\DEV\MvcSample\mvc1\Views\Home\AboutUs.aspx

Wrote: C:\DEV\MvcSample\mvc1\Views\Home\AboutUs.aspx.cs

Wrote: C:\DEV\MvcSample\mvc1\Views\Home\AboutUs.aspx.designer.cs

Wrote: C:\DEV\MvcSample\mvc1\Views\Home\Contact.aspx

Wrote: C:\DEV\MvcSample\mvc1\Views\Home\Contact.aspx.cs

Wrote: C:\DEV\MvcSample\mvc1\Views\Home\Contact.aspx.designer.cs

We just generated a "controller set" of files for a controller called "Home" and three actions, Index, AboutUs and Contact....

OK, now jump back into Visual Studio, show all files and include the new files.


Now we have a generated "controller set":

(Note that the menu code in the master page is out of wack. Menu items at this level are usually cross controller or make use of multiple master pages etc...)

Now how about another set of files. Try a members area, some actions could be index, list, search, inbox, send message.

C:\DEV\MvcSample>AspNetMvcGenerator.exe controllerset Members Index List Search Inbox SendMessage

Wrote: C:\DEV\MvcSample\mvc1\Controllers\MembersController.csWrote: C:\DEV\MvcSample\mvc1\Views\Members\Index.aspxWrote: C:\DEV\MvcSample\mvc1\Views\Members\Index.aspx.csWrote: C:\DEV\MvcSample\mvc1\Views\Members\Index.aspx.designer.csWrote: C:\DEV\MvcSample\mvc1\Views\Members\List.aspxWrote: C:\DEV\MvcSample\mvc1\Views\Members\List.aspx.csWrote: C:\DEV\MvcSample\mvc1\Views\Members\List.aspx.designer.csWrote: C:\DEV\MvcSample\mvc1\Views\Members\Search.aspxWrote: C:\DEV\MvcSample\mvc1\Views\Members\Search.aspx.csWrote: C:\DEV\MvcSample\mvc1\Views\Members\Search.aspx.designer.csWrote: C:\DEV\MvcSample\mvc1\Views\Members\Inbox.aspxWrote: C:\DEV\MvcSample\mvc1\Views\Members\Inbox.aspx.csWrote: C:\DEV\MvcSample\mvc1\Views\Members\Inbox.aspx.designer.csWrote: C:\DEV\MvcSample\mvc1\Views\Members\SendMessage.aspxWrote: C:\DEV\MvcSample\mvc1\Views\Members\SendMessage.aspx.csWrote: C:\DEV\MvcSample\mvc1\Views\Members\SendMessage.aspx.designer.cs

Again, show all files and include the new controller and view folder. You just got another huge kick-start :-)

The Templates

The main reason that the templates are copied along with the generator executable is that you will tend to tweak them for the individual project – especially the view ones. They are based directly off the standard VS.NET MVC Preview 5 templates. This is partly so that maintenance is easy (a good thing). The only one I added was the view page content (ViewContentPageContent.txt) and a token in the view page ($ContentTags$).

Note that I do need to include the VB stuff yet...

Other Enhancements

Firstly I'll get the VB issues sorted...

One thing I would like to do is add some smarts for the actions – something like if the action is "List" then the controller will generate some basic LINQ against a model. The views could spit out a simple foreach loop or include a control etc. They are not a big priority though seeing as you can code them pretty quick anyway! It would only be if you were working on a real big site.


Anyway, let me know what you think, kick it, link it - whatever, all feedback is good!

kick it on (12.86 kb) / (29.54 kb)

Comments (3) -


Looks like a it is worthy to try. I don't know rails but i will try this definitively.

Dan Atkinson

This is a pretty good idea, and a great project!

I would recommend implementing a UI for ease of use, and also have an option of updating the project xml file automatically, so that you don't need to do show all, and include them. Also, I'd stick the idea on Codeplex to allow others to help you and to expand it to other areas.


Thanks Yazılım/Dan,

Theres a bit more ironing out to do and I am considering codeplex (I have 1 project there already). I planned on the UI for (with perhaps a /gui option) and considered a visual studio plugin too. The trick is not to get bogged down in the details!

PK Smile

Comments are closed