mdm
mdm

Reputation: 43

Add MVC to an existing ASP.net website

I've searched the archives and also googled about it but it seems I can't find a guide / best practices / how-to on how to smoothly add features to an existing ASP.net 4 website, adding them in the latest MVC. I have a website that was built with ASP.net 1.0 and progressively has been upgraded to 4.0, now I want to switch to MVC but re-create all the website in MVC is a too long process since the site has a lot of features, and some parts are just good in webforms. The best way for me should be to be able to smoothly add MVC to this existing ASP.net 4.0 website and progressively migrate the existing webforms to MVC.

(Don't want to start a webforms-MVC flame, just looking on some tips on how to avoid common mistakes.)

Upvotes: 4

Views: 1781

Answers (1)

artwl
artwl

Reputation: 3582

  1. add System.Web.Mvc reference in ASP.net website
  2. for support MapRoute,you need add the following code in web.config:

<system.webServer>
 <validation validateIntegratedModeConfiguration="false"/>
 <modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>

 3. add assembly reference in web.config:


<compilation defaultLanguage="c#" debug="false" targetFramework="4.0">
    <assemblies>
    <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    <add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </assemblies>
</compilation>
4. add routes map in Global.asax,such as:


public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    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);
}

Upvotes: 2

Related Questions