For those of you doing MVC out there you've probably noticed that every time you create a new view within your project, the template that VS uses creates a both a .aspx.cs and a .aspx.designer.cs file for your view.  A group of mvps/insiders have been talking about this topic for a bit.  A small group of us feel that providing this file is a temptation that can lead to world of hurt.

To us, the this code behind file is a honeypot asking for bad practices.  The fact that you have a codebehind file and that you can put ANY type of code within it, easily negate the fact that you're using MVC.  Why?  A developer can easily put non-view code within this file, after all it is the codebehind for a webform.  Non-view specific code can range from db connections, LINQ queries, or even worse web service calls.  Fellow Insider/MVP Steven Smith had an excellent blog post entitled,

So Rule of Thumb #1 for ASP.NET MVC, DO NOT USE COBEHIND FILES IF YOU'RE USING THE WEBFORM VIEW ENGINE.

Having said that, you'll need tooling support to prevent you from going into these wicked ways.  So, what I've taken the liberty to change the ItemTemplates that ship with MVC Beta 1 and changed them to prevent the creation of a code behind for both Views and Master pages.  To get these templates, go to:

Here's what you need to do:

  • Make sure that VS is not loaded
  • For the item template zip files
      • MvcViewContentPageItemTemplateBeta.cs.zip
      • MvcViewMasterPageItemTemplateBeta.cs.zip
      • MvcViewPageItemTemplate.cs.zip
      • MvcViewPageItemTemplateBeta.cs.zip
    • Copy these files to these locations:
      • C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC\103
      • C:\Program Files\Microsoft ASP.NET\ASP.NET MVC Beta\Temp

 

  • For the itemtemplatecache_folders.zip file
    • Extract the contents (should be 4 folders of the previous 4 item template zip files).
    • Copy these folders to:
      • C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplatesCache\CSharp\Web\MVC\1033

After you've completed these steps you can get a codebehind-less MVC View within your project from these dialogs:

image

New Add View Dialog

image

Add New Item Dialog

If you have any questions or need help installing these new templates, please feel free to leave a comment!