Homepage | About Me | Mayday | Testing ASP.net Book | Follow me on Twitter | GitHub | SlideShare | RSS | DropBox referral link
Blog.BenHall.me.uk

ASP.net MVC RC1 – Removing Code Behind files

Wednesday, January 28, 2009

With ASP.net MVC, one thing I always wondered is why we still had to have the code behind files. Looking at the FubuMVC samples, everything looks much cleaner without the additional files – with RC1, code-behind files are no longer required!

However, if your upgrading from beta, you will need to change your ASPX pages. In this post, I will cover the steps I went through to remove these files.

During beta, my solution looked looked like this:

image

In my code behind, I have a mixture of strongly typed ViewPage<T> objects and the standard ViewPage. As a result, in my ASPX page, I had a CodeBehind file and a Inherits tag.

image

With RC1, I can remove the CodeBehind tag and replace the Inherits to System.Web.MVC.ViewPage directly.

image

I can now delete Index.aspx.cs and Index.aspx.designer.cs leaving me with just the Index.aspx file.

If my ViewPage was strongly typed with a model, the the Inherits tag would include this, just like with the code behind model:

image

For ASP.net MVC User Controls, instead of ViewPage<T> you use ViewUserControl<T>:

image

Similarly with Master Pages:

image

After changing all my views, controls and master pages, my solution looks like this:

image

This makes me happy.

UPDATE: Parser Error Message: Could not load type 'System.Web.Mvc.ViewPage<ProductsMenuModel>'.

Ok – I made a little mistake. When I hit my ViewPage<T>, I received a Parser error message. If I had paid more attention to the release notes, I would have noticed I needed to copy an additional block of xml into my web.config.

<pages
     pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
     pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
     userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
       <controls>
            <add assembly="System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
       </controls>
</pages>

Technorati Tags:

Labels:

Blogger comments

At 12:36 PM, Blogger Paul Lockwood said...
Not having code behind files makes our lives so much easier when teaching MVC. Before the RC people were saying "well I they are there so we can use them".    
At 4:56 PM, Blogger Tommy said...
Thanks for posting this up! Saved me a few minutes trying to figure out what I was doing wrong :)    
At 7:26 PM, Blogger bgreig said...
This comment has been removed by the author.    
At 7:27 PM, Blogger bgreig said...
Thanks, that worked!    
At 4:13 PM, OpenID DaveTheNinja said...
Cheers ben, I too overlooked the RC notes, however I never overlooked your blog post :-)

Dave the Ninja    
Thanks for the tip on the error in your update comment. In my dev. environment everything worked, but in production these missing lines in web.config made it fail.    
At 8:52 AM, Blogger Malik said...
Thanks a zillion for figuring out that parser error. It was killing me tonight while I was trying to convert my blog platform to MVC 1.0 (www.blogpub.com). I seem to always skip reading instructions so who knows how long it would of took me to figure that out. done!