First time here? You are looking at the most recent posts. You may also want to check out older archives or the tag cloud. Please leave a comment, ask a question and consider subscribing to the latest posts via RSS. Thank you for visiting! (hide this)

Currently I’m in the process of upgrading all the samples I wrote from my Beginning ASP.NET MVC 1.0 book from the Beta to the RTM version of ASP.NET MVC. The biggest update is taking advantage of the views without code-behind. This is not something you have to do on a “real application” since the old way will work also on the RTM, but since my samples are made to show how to develop an ASP.NET MVC application, I hade to upgrade them all to the “correct” way of doing things.

The first step is pretty easy: just download the .cs and .designer.cs files that are nested behind the view.

Then you have to change the View Page directive removing the CodeBehind attribute and changing the Inherit one to System.Web.Mvc.ViewPage.

<%@ Page
    Language="C#"
    MasterPageFile="~/Views/Shared/Site.Master"
    AutoEventWireup="true"
    CodeBehind="Index.aspx.cs"
    Inherits="SampleApp.Views.Home.Index" %>

The code above must be changed to:

<%@ Page
    Language="C#"
    MasterPageFile="~/Views/Shared/Site.Master"
    Inherits="System.Web.Mvc.ViewPage" %>

And everything works great with loosely typed views.

If you have to upgrade a strongly typed view, you have to change the Inherits attribute to the generic version of ViewPage (and don’t forget to import the namespace where the presentation model object is).

<%@ Page
    Language="C#"
    MasterPageFile="~/Views/Shared/Site.Master"
    Inherits="System.Web.Mvc.ViewPage<MyDto>" %>
<%@ Import Namespace="SampleApp.Models"%>

But if you run the page you get the following error:

Could not load type 'System.Web.Mvc.ViewPage<MyDto>.

This is because the ASP.NET compiler cannot understand the generic syntax with angle brackets, but only the ugly backtick one (ViewPage`1[MyDto]).

It took me a while to understand how to solve the problem, and I ended up making an app from scratch and comparing the two folders. To make this work you have to add, in the web.config file that is inside the Views folders, the following section (actually, replacing the one that is there already, text wrapped to enhance readability):

<pages
    validateRequest="false"
    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>

This tells the ASP.NET compiler to use the ASP.NET MVC specific parse filter that understands the angle bracket syntax.

HTH

Technorati Tags:
posted on Tuesday, April 7, 2009 9:07 PM

Comments on this entry:

# re: One little gotcha to remember when upgrading to ASP.NET MVC RTM

Left by irocon at 4/8/2009 5:04 PM

First comment.. yay!
Thanx, been looking at some of the samples. great tip.

# re: One little gotcha to remember when upgrading to ASP.NET MVC RTM

Left by Liam McLennan at 4/8/2009 10:34 PM

I believe that was in the release notes.

# re: One little gotcha to remember when upgrading to ASP.NET MVC RTM

Left by Sruly Taber at 5/31/2009 6:27 PM

Thanks so much. I have been having a problem with strongly typed views ever since I upgraded and couldn't figure it out. I just changed the web.config and it works like a charm.

Thanks

# re: One little gotcha to remember when upgrading to ASP.NET MVC RTM

Left by Anders Juul at 6/8/2009 11:38 AM

Excellent - helped me out!
Much appreciated,
Anders, Denmark

Comments have been closed on this topic.