Long Past Due Project Upgrades

by Matt 14. September 2011 14:59

I spent a great deal of time this weekend upgrading a project from .NET 3.5, MVC 2.0, and ASPX View Engine to .NET 4.0, MVC 3.0, using the Razor view engine, and implemented Telerik's RadControls for ASP.NET Ajax.  In other words, it was a great weekend! Smile

First - some weeks prior to this, I had upgraded the same project from MVC 1.0 to MVC 2.0.  I personally did this manually, but there is a tool to automate the process available for free download here.  Anyway, back to this past weekend...

The first thing I did was make a backup, and you should too, via some versioning system or by making a copy of your solution folder.  The next step of this was to convert the project from .NET 3.5 up to .NET 4.0.  I did this by opening the solution in Visual Studio 2010 and let the converter work its magic.  There were no conversion errors, so the first part of this was done.  Next step was downloading and installing MVC 3.0.

After that, I wanted to convert my MVC 2.0 project to MVC 3.0.  Having just done a manual conversion of this same project weeks before, I decided this time I would treat myself to the simplicity of using the converter tool this time. 

After loading my solution I confirmed the project referenced the latest System.Web.Mvc.dll.  I made a few small configuration changes and tested my website, paying particular attention to my custom routing and postbacks.  Everything worked, which actually took me by surprise.  Curious about what my web.config looked like after the upgrade, I took a peek:

 

 

Code Snippet
  1. <runtime>
  2.     <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  3.       <dependentAssembly>
  4.         <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
  5.         <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
  6.       </dependentAssembly>
  7.     </assemblyBinding>
  8.   </runtime>

 

 

Perfect - this will ensure all references are redirected to the latest assembly.

Now came the fun part.  I've wanted to explore a view engine other than the WebForm View Engine for quite some time, particularly the Razor view engine.  Razor ships with MVC 3.0 so it was already installed - I just had to create a new view as I normally would by right clicking the ActionView in the Controller class and selecting 'Add View'.  At the top of this form I gave the view a name, and the control immediately below that allows me to select a view engine; of course, I picked Razor.

Converting my WebForms views to Razor views was fun.  I knew nothing of the syntax, so I used Phil Haack's comparison as a starting point.  From there I sort of 'figured it out', converting each view one at a time, first the basic ones, then along to the harder ones.  To date, all my 'controller' views and my master page have been converted; I'll convert the partial views when I get more time, likely this coming weekend.

All in all that was a great success, and I really admire the view pages.  Razor syntax is so clean that even the complex view pages look great.  Take this simple comparison between the two different view types (photos courtesy of The Gu):


WebForms View Engine Syntax:
WebForms View Engine Syntax

Razor View Engine Syntax:
Razor View Engine Syntax

To represent the same loop that Razor represents with three nuggets, WebForms view engine takes four sets of nuggets!  I find this looks much more attractive.  Razor is smart enough to close the loop and code blocks, and to distinguish the code blocks from HTML.

I did have more fun this weekend as well - Implementing Telerik controls on a few pages to provide an aesthetically pleasing way of displaying some data.  But that will have to wait for a future post.

***

Earlier today, I caught wind of MVC 4.0 Developer Preview being released.  It seems I can never get caught up!