Topic: Convert Web App for MVC Framework
Share/Save/Bookmark
Description: This tutorial will show you how to convert an ASP.Net Web application to use the Microsoft MVC Framework.
Notes:

1.     This tutorial assumes you already have Profile Providers in your web application for roles and membership.

2.     You have to have Framework 3.5 SP1 or have installed VS2008 SP1.

3.     There is also the assumption that you have a Web Application and not a Web Site project.  If you have a Web Site project, you can search the web on how to convert it to a Web Application.  You can start with the following link: http://webproject.scottgu.com/CSharp/Migration2/Migration2.aspx


First, Start by adding the following project references:

- System.Web.Abstractions,

- System.Web.Mvc,

- System.Web.Routing


Next, modify your “web.config” file

Under <configuration><system.web><compilation><assemblies> add the following assemblies:

<compilation debug="true">

  <assemblies>

    <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>

    <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

   <add assembly="System.Web.Abstractions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

   <add assembly="System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

   <add assembly="System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

   <add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>

   <add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>

   <add assembly="System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>

  </assemblies>

</compilation>


Under <configuration><system.web><pages><namespaces> add the following namespaces:

<namespaces>

  <add namespace="System.Web.Mvc"/>

  <add namespace="System.Web.Mvc.Ajax"/>

  <add namespace="System.Web.Mvc.Html"/>

  <add namespace="System.Web.Routing"/>

  <add namespace="System.Linq"/>

  <add namespace="System.Collections.Generic"/>

</namespaces>

 

Note: You may have to add the <namespaces> element.


Under <configuration><system.web><httpHandlers> add the following HttpHandler:

<httpHandlers>

  <remove verb="*" path="*.asmx"/>

  <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

  <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

  <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>

  <add verb="*" path="*.mvc" validate="false" type="System.Web.Mvc.MvcHttpHandler, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

</httpHandlers>



Under <configuration><system.web><httpModules> add the following HttpModules:

<httpModules>

  <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

  <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

</httpModules>


Under <configuration><system.webServer><modules> add the following modules:

<modules runAllManagedModulesForAllRequests="true">

  <remove name="ScriptModule"/>

  <remove name="UrlRoutingModule"/>

  <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

  <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>



Under <configuration><system.webServer><handlers> add the following handlers:

<handlers>

  <remove name="WebServiceHandlerFactory-Integrated"/>

  <remove name="ScriptHandlerFactory"/>

  <remove name="ScriptHandlerFactoryAppServices"/>

  <remove name="ScriptResource"/>

  <remove name="MvcHttpHandler"/>

  <remove name="UrlRoutingHandler"/>

  <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

  <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

  <add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

  <add name="MvcHttpHandler" preCondition="integratedMode" verb="*" path="*.mvc" type="System.Web.Mvc.MvcHttpHandler, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

  <add name="UrlRoutingHandler" preCondition="integratedMode" verb="*" path="UrlRouting.axd" type="System.Web.HttpForbiddenHandler, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>

</handlers>


Next the Global.asax file needs modified to allow routing to work.  You need to add a class to your web application called “Global.asax.cs” and inherit from HttpApplication. 

 

Once this is done, you’ll need to move any methods from the original “Global.asax  to the new “Global.asax.cs” file. 

 

After your “Global.asax.cs” is added, change the “Global.asax” to the following:

 <%@ Application Codebehind="Global.asax.cs" Inherits="MyWebApplicationNamespace.Global" Language="C#" %>

 

If you had any Role Provider code or Configuration code, you will need to add the following using statements:

using System.Configuration;

using System.Web.Security;

The following examples show you how the two files should look:

Global.asax

<%@ Application Codebehind="Global.asax.cs" Inherits=" MyWebApplicationNamespace.Global " Language="C#" %>


Global.asax.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using System.Web.Routing;

 

namespace MyWebApplicationNamespace

{

  public class Global : HttpApplication

  {

    public static void RegisterRoutes(RouteCollection routes)

    {

      routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

      routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");// prevents aspx page rounting by MVC

 

      routes.MapRoute(

        "Default",                    // Route name

        "{controller}/{action}/{id}", // URL with parameters

        new { controller = "Home", action = "Index", id = "" }  // Parameter defaults

        );

    }

 

    protected void Application_Start()

    {

      RegisterRoutes(RouteTable.Routes);

    }

  }

}


Next add the folders to your Web Application for the required MVC framework:

-    /Controllers

/Views

-     /Views/Home

-      /Views/Shared (optional folder good for MasterPages and LoginControls)


Next add a new web form called “Index.aspx” to your “Views/Home” folder.  You will need to delete the code behind file, as well as, the designer.cs file.  In the markup of the page, you’ll need to remove the “CodeBehind” reference and change the “Inherits” attribute to “System.Web.Mvc.ViewPage”.  An example page is below:

<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

  <head runat="server">

    <title>My MVC Home Index Page</title>

  </head>

<body>

  <div>

    <h1>

      <%= Html.Encode(ViewData["MyData"]) %>

    </h1>

  </div>

</body>

</html>


Next, you’ll add a new controller to the Controllers folder called “HomeController.cs”.  The “Global.asax” defines the path and requires the Controller to exist.  An example of the HomeController is below:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

 

namespace MyWebApplicationNamespace.Controllers

{

  public class HomeController : Controller

  {

    public ActionResult Index()

    {

      ViewData("MyData") = "Hello David!”;

      return View();

    }

  }

}


Lastly, build your Web Application and test your code.