Tuesday, 21 June 2011

Upgrade ASP.Net MVC2 to MVC3

Having recently upgraded a couple of ASP.Net MVC 2 projects to MVC 3, I came across a couple of tools and techniques to simplify the process.

ASP.Net MVC3 requires .Net 4 and hence use of VS.Net 2010 (or express editions) are required. There's also an additional download of the MVC3 framework required too.

One of the simplifying changes of the upgrade is a slimmed down web.config file - but I found this meant an in-place upgrade of this and all the necessary references rather complicated. So instead I created a new, empty MVC3 project and copied over all the controllers, models, views and necessary web.config settings into the new project.

An important security update is to HTML encode all output by default, using the new <:= > syntax (as opposed to <%= %>). If you've been encoding output properly in previous versions any potentially insecure content would have been wrapped in Html.Encode methods. But updating all of these to the new syntax looks to be a pretty time consuming and tedious task.

So was very pleased to discover this tip, which details how VS.Net's find/replace syntax can be used to make the update across the project.

Having done that though, what I really wanted to do was make use of the new Razor view engine. But again, updating all the views to use this syntax would take quite some time working manually.

This time a tool produced by Telerik proved invaluable in carrying out this task. You can run it on a folder of view files that use the web forms view engine, and it will convert them into ones using Razor. It's not 100% perfect - I found in particular a few brackets need manually adding here and there to ensure valid syntax - but does most of the job and saves an awful lot of time.