Saturday, February 12, 2011

Converting ASP.Net MVC 2.0 application to 3.0

Recently I converted medium size project to ASP.Net MVC 3.0. In parallel I decided to move from web forms view engine to the Razor as it is wordless and more clean. In this post I would like to share my experience and show problems which I found during migration.

First of all check the following article of Scott Guthrie: Announcing release of ASP.NET MVC 3, IIS Express, SQL CE 4, Web Farm Framework, Orchard, WebMatrix. In the “How to Upgrade Existing Projects” he says:

ASP.NET MVC 3 is compatible with ASP.NET MVC 2 – which means it should be easy to update existing MVC projects to ASP.NET MVC 3

It is generally true for MVC itself. But in real life applications there are a lot of 3rd party libraries which should be also compatible with new MVC version. Also if you perform migration from web forms view engine to Razor there will be another problems. So generally migration process is not so easy and straightforward.

For begin check manual upgrade guide available from here (there is also automated ASP.NET MVC 3 upgrade tool available for download, but I don’t really like these kind of tools because I want to know what I need to do and why I need to do it). Start with the steps described in the manual upgrade guide. Note that there is typo in step 6: instead of System.WebPages.dll you need to add System.Web.WebPages.dll.

After you have performed all steps described in the guide try to compile your application. Most probably 1st compilation will fail. In my case it was caused by incompatibility with another libraries:

MVCContrib assembly version for MVC 3.0 is available for download from codeplex. For MVCFutures (Microsoft.Web.Mvc.dll) the process is not straightforward: I didn’t find binary version available for download on the There is ASP.NET MVC 3 Beta release which contains link on ASP.NET MVC 3 Beta Futures. But unfortunately beta version is not compatible with final release of MVC 3. So the only way I found was to download sources from ASP.NET MVC 3 RTM and build it by myself.

At the moment of writing this article MvcSiteMapProvider  was also released for MVC 3.0 (date of release is 4 Feb 2011). When I started migration there were no version for MVC 3 available – fortunately MvcSiteMapProvider for MVC 2 also works in my scenarios.

Telerik extensions are available for MVC 3.0 at the moment of writing this article. Do not forget that for Telerik extensions with Telerik.Web.Mvc.dll you also need to add css and scripts for appropriate version (these files are located in appropriate folder with version number name, e.g. 2010.3.1318).

Now about converting from web forms view engine to Razor. There is Razor converter tool available from Telerik. I recommend to use this tool as it will make most of simple transformations automatically. Although it has problems, so you will still need to perform manual work. I found the following problems during migration:

  • doesn’t convert placeholders correctly
  • If you use strong typed ActionLinks from MVCFutures:
    @Html.ActionLink<RequestController>(c => c.Create(), ...)
    you will need to add parenthesis after @ for each expression by yourself, e.g.:
    @(Html.ActionLink<RequestController>(c => c.Create(), …))
  • ignores Import directives, so you have to do it manually:
    <%@ Import Namespace="UI.Controllers" %>
    @using UI.Controllers
  • doesn’t convert server scripts in html tags and javascript variables, so you will also need to do it manually:
    <img alt="" src="<%= Url.Content("~/content/images/foo.gif") %>" />
    <img alt="" src="@Url.Content("~/content/images/foo.gif")" />

In addition to the mentioned problems you may face with situation that some html helpers which worked for web forms view engine doesn’t work for Razor. E.g. Grid from MVCContrib has known limitation: action syntax is not working for Razor. From twitter conversation with Jeremy Skinner (author of MVCContrib grid): the workaround is to replace action syntax with custom columns.

That’s all what I wanted to share with you in this post. Hope it will help you in your work.

1 comment:

  1. You might consider reporting the converter limitations to its issues page on github :) This way more people will know about them and might even submit patches!