Pages

Friday, September 16, 2011

ASP.NET MVC - Upgrading an Existing Preview2 Application to Preview 3

The information in this section describes the changes you must make to modify an ASP.NET MVC application that was created with the Preview 2 release so that it works with the Preview 3 release.

Code Changes

· Update the references to the following assemblies to point to the new Preview 3 versions of the assemblies:
  • · System.Web.Abstractions
  • · System.Web.Routing
  • · System.Web.Mvc
By default, these assemblies are located in the following folder:

%ProgramFiles%\Microsoft ASP.NET\ASP.NET MVC Preview 3

  • For all existing action methods, change the return type from void to ActionResult.
  • Anywhere you call RenderView, change it to a call to return View. You can search for RenderView( and replace it with return View(.
  • Anywhere you call RedirectToAction, prepend the call with the return keyword. Search for RedirectToAction( and replace it with return RedirectToAction(.
  • If you use a strongly typed page, replace <%= ViewData.PropertyName %> with <%= ViewData.Model.PropertyName%>. Rather than replacing the ViewData object with your strongly typed object, the MVC framework now sets the Model property to the instance that you provide.
  • In the Global.asax file, remove the route definition for Default.aspx. In the default Preview 2 template, the route looked like the following example:
routes.Add(new Route("Default.aspx", new MvcRouteHandler())
{
  Defaults = new RouteValueDictionary(new { controller =  "Home", action = "Index", id = "" }),
});
  • In the Global.asax file, find the following default MVC route:
routes.Add(new Route("{controller}/{action}/{id}", new MvcRouteHandler())
{
Defaults = new RouteValueDictionary(new { action = "Index", id = "" }),
});
Replace it with the following route:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
  • Add the following line at the very beginning of the RegisterRoutes method:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  • Edit the Default.aspx file and add the following line:
<% Response.Redirect("~/Home") %>

This redirect is not necessary for IIS 7. This is a workaround for an issue with how the Web server that is built into Visual Studio (the ASP.NET Development Server) works with routing.

Configuration Changes

  • In the Web.config file, you must change the type attribute of the httpHandler entry in the section for UrlRoutingHandler to System.Web.HttpForbiddenHandler. To do this, search for the following string in the file:
path="UrlRouting.axd" type="System.Web.Routing.UrlRoutingHandler, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
Replace it with the following string:
path="UrlRouting.axd" type="System.Web.HttpForbiddenHandler, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
  • Because the version numbers of the System.Web.Abstractions and System.Web.Routing assemblies have been changed to 0.0.0.0, you must update version information in the Web.config file. In the Web.config file, search for the following string:
System.Web.Routing, Version=3.5.0.0
Replace it with the following string:
System.Web.Routing, Version=0.0.0.0
Search for the following string:
System.Web.Abstractions, Version=3.5.0.0
Replace it with the following string:
System.Web.Abstractions, Version=0.0.0.0

http://www.asp.net/mvc