Saturday, February 26, 2011

Converting MVC 2.0 projects to MVC 3 (Razor)

Microsoft has outlined the steps to convert a project from MVC 2 to 3 here:

Upgrading an ASP.NET MVC 2 Project to ASP.NET MVC 3


You also have the option of using a tool to do this, see
ASP.NET MVC 3 Application Upgrader


It may be easiest to simply import all your files into the new project and migrate each web.config section over. However - if you do decide to convert your projects over there are two additional things to keep in mind.

After upgrading you should
1. Be aware that keeping the existing binding redirect that exists in 2.0 projects to force all 1.x-2.0 System.Web.MVC libraries to bind to MVC 2.0 can cause your worker process to crash. This was in 2.0 projects by default. You MUST remove this (replace it with a binding to 3.0 and not 2.0)

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>

You should _replace_ it with the following

<runtime>
<assemblyBinding
xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity
name="System.Web.Mvc"

publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect
oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>




2. Let's say all of my views use the MVCContrib grid and the Microsoft Futures or other features. Rather than having import statements on all of your razor views such as:

@using MvcContrib.UI.Grid;
@using Microsoft.Web.Mvc


You can simply include these imports in the web.config as you can with web forms projects, but the section to include them is different for mvc 3.

first add this to the <configuration><configSections> section

<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>

Now add this section under your <configuration>

<system.web.webPages.razor>
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="MvcContrib.UI.Grid" />
<add namespace="Microsoft.Web.Mvc" />
</namespaces>
</pages>
</system.web.webPages.razor>



See it all here:

2 comments:

  1. In sukere InfoTech we bolster a full chain advancement prepare from necessities definition, specification, engineering plan, coding, testing, approval, upkeep and support. Depending on your particular need our expert will take you through every period of arrangement giving you a sound direction on innovation and application choices.

    web outlining in Chennai -Sukere infotechs

    ReplyDelete