[ASP.NET MVC]Build ASP.NET MVC Project Upon Classic Web Form Project

1. Install MVC(System.Web.Mvc.dll)(http://www.asp.net/mvc/)

MVC depend on System.Web.Abstraction.dll; System.Web.Mvc.dll; System.Web.Routing.dll. while System.Web.Abstraction and System.Web.Routing.dll are already installed with .Net Framework 3.5.we can find these 2 assemblies in GAC.

2. Create a classic Web Form project - Add some folders,add references to 3 dlls

Controllders and Views folders are needed in a Mvc project.

3. Alter <system.web> of  Web.config

   <system.web>
      <compilation debug="false">
         <assemblies>
            <add assembly="System.Core, Version=3.5.0.0,
                           Culture=neutral,    
                           PublicKeyToken=B77A5C561934E089"/>
            <add assembly="System.Data.DataSetExtensions, 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.Xml.Linq, Version=3.5.0.0,
                           Culture=neutral,
                           PublicKeyToken=B77A5C561934E089"/>
            <!--for mvc-->
            <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"/>
            <!--for mvc-->
         </assemblies>
      </compilation>
      <authentication mode="Forms" />
   
      <pages>
        <controls>
          <add tagPrefix="asp" namespace="System.Web.UI"
                assembly="System.Web.Extensions,              
                    Version=3.5.0.0,               
                    Culture=neutral,
                    PublicKeyToken=31BF3856AD364E35"/>
          <add tagPrefix="asp" namespace="System.Web.UI.WebControls"     
                               assembly="System.Web.Extensions,
                                         Version=3.5.0.0,
                                         Culture=neutral,
                                         PublicKeyToken=31BF3856AD364E35"/>
        </controls>
        <!--for mvc-->
        <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>
        <!--for mvc-->
      </pages>
   
      <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"/>
        <!--for mvc-->
        <add verb="*" path="*.mvc" validate="false"
                      type="System.Web.Mvc.MvcHttpHandler,
                            System.Web.Mvc,
                            Version=1.0.0.0,
                            Culture=neutral,
                            PublicKeyToken=31BF3856AD364E35"/>
        <!--for mvc-->
      </httpHandlers>
 
      <httpModules>
        <add name="ScriptModule" type="System.Web.Handlers.ScriptModule,
                                       System.Web.Extensions,
                                       Version=3.5.0.0,
                                       Culture=neutral,
                                       PublicKeyToken=31BF3856AD364E35"/>
        <!--for mvc-->
        <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule,
                                       System.Web.Routing,
                                       Version=3.5.0.0,
                                       Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <!--for mvc-->
      </httpModules>
   </system.web>

4. To run MVC web site in IIS7

    set "runAllManagedModulesForAllRequests=ture" see the bold part
   <system.webServer>
      <validation validateIntegratedModeConfiguration="false"/>
      <modules runAllManagedModulesForAllRequests="true">
        <remove name="ScriptModule" />
        <add name="ScriptModule" preCondition="managedHandler"
             type="System.Web.Handlers.ScriptModule, System.Web.Extensions,
                   Version=3.5.0.0,
                   Culture=neutral,
                   PublicKeyToken=31BF3856AD364E35"/>
        <!--for mvc-->
        <remove name="UrlRoutingModule" />
        <add name="UrlRoutingModule"
            type="System.Web.Routing.UrlRoutingModule, System.Web.Routing,
                  Version=3.5.0.0,
                  Culture=neutral,
                  PublicKeyToken=31BF3856AD364E35" />
        <!--for mvc-->
      </modules>
      <handlers>
        <remove name="WebServiceHandlerFactory-Integrated"/>
        <remove name="ScriptHandlerFactory" />
        <remove name="ScriptHandlerFactoryAppServices" />
        <remove name="ScriptResource" />
        <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" />
        <!--for mvc-->
        <remove name="MvcHttpHandler" />
        <remove name="UrlRoutingHandler" />
        <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" />
        <!--for mvc-->
      </handlers>
    </system.webServer>

5. Add Global.asax file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.Mvc;
using System.Web.Routing;
   
namespace WebFormWithMVC {
    public class Global : System.Web.HttpApplication {
   
        protected void Application_Start(object sender, EventArgs e) {
            RegisterRoutes(RouteTable.Routes);
        }
        //////////////////////////////////////////////////////////////
        // the following is customized methods for mvc
        //
        public static void RegisterRoutes(RouteCollection routes) {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
            routes.MapRoute("Default", "{controller}/{action}/{id}",
                             new { controller = "Home", action = "Index", id = "" });
        }
    }
}

6. Create a Controller

   Under the Controller folder, create a HomeController.cs file:
 
using System.Web.Mvc;
namespace WebFormWithMVC.Controllers {
    public class HomeController : Controller {
        public ActionResult Index() {
            ViewData["Message"] = "This is ASP.NET MVC!";
            return View();
        }
    }
}

7. Create a View Page - Index.aspx

Under folder : Views/Home
<%@ Page Language="C#"  Inherits="System.Web.Mvc.ViewPage" %>
<html>
    <head>
    </head>
    <body>
        <%=ViewData["Message"]%>
    <body>
</html>

8.Deploy web project to IIS7 and test it

http://localhost/MVCWith3Dlls/Home/Index

blog comments powered by Disqus