Upgrade to MVC 3 RC 2
Sun Dec 19 2010 by MarkP
Things move fast in the MVC world. After just recently upgrading to MVC 2.0, this site has now been upgraded to Release Candidate 2 of the ASP.NET MVC framework Version 3. One of the major additions to the framework is the option to use the new Razor View Engine when creating Views. It never felt right using Master Pages in MVC I suppose it was thought of as transitional phase from migrating from ASP.NET forms to MVC. The ASP.NET view engine can still be used if wanted but after upgrading from this to the Razor View Engine can see the benefits of switching. I know that a lot of development has been undertaken using the Spark View Engine due to it being more HTML friendly and improved readability. Razor has addressed a lot of these issues and will be interested as to how this will effect the number of users now implementing Spark.
One the first "gotchas" was distinguishing between encoded and non encoded HTML. Razor automatically encodes the text when using the @ prior to an HTML helper class.
This caused me issues when trying the display the content of the blog text as this can include HTML markup. A way round this is to either use MvcHtmlString.Create method or as from RC 2
the Html.Raw helper method.
This also caused me an issue when trying the generate the Recaptcha control - this now returns a MvcHtmlString from a custom helper I created rather than a string.
In my previous version of the site I was using standard inline code to generate hyperlinks - this has now been updated to use Html.ActionLinks. This was an oversight in my part when initially developing the site but once implemented in Razor, implementing this way solved some of the compilation issues encountered.
Below is a brief overview of the steps used in upgrading the website application in Visual Studio 2010
- Added a new MVC 3 Web application to the main solution and selected Razor as the View Engine
- Removed any unwanted files created when the application was added.
- Copied all View, Controllers and associated directories from previous MVC 2 application to MVC 3 application.
- Copy any sections from my web.config to the web.config in the new MVC 3 application
- Renamed all Views to used the .cshtml extension.
- Renamed the Master Page to _Layout.cshtml.
- Removed all references to the Master Page content templates and use the @model directive to call the relevant layout pages in the Views
- Refactor all <%= %> and <% %> tags to use the correct Razor syntax (@) - this required quite a bit of work to ensure the syntax was correct.
- In the previous version I was using user controls (.ascx files) which I replaced using .cshtml files and Partial Views.
- Remove the previous MVC 2 website application and set the MVC 3 version as the website application
When deploying the site I had to ensure that all the new assemblies were included in the bin directory of the website. When running locally this is not an issue as the assemblies are automatically included in the GAC. My host provider Discountasp.net currently do not have these installed on their servers so these files had to be included for the website to work correctly.
After a few tweaks I got the site up and running and is working as expected. All I am hoping now there are not many changes when the full release version of MVC 3 is released.