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
    Inherits="SampleApp.Views.Home.Index" %>

The code above must be changed to:

<%@ Page
    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
    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):

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

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


Technorati Tags: