Convert ASP.Net webform to ASP.Net MVC

There are several approaches to this:

First approach

Add a MVC project to your solution where you already have your ASP.Net webforms website / web application project.

Work on your MVC project separately, code, test, run.

When you’re done, compile and copy the Views folder.

Change the output path for the assembly from the local bin directory to the bin directory of my web site. This assembly contains the Controllers and Models.

Add routing to your website.

In Global.asax:

using System.Web.Mvc;
using System.Web.Routing;

Also:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}

public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

// your routes
}

In Aplication_Start:

AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);

In web.config:

<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>

Second approach

Create Controllers and Views folder on your website

Setup/configure the routes in the global.ascx file.

Change to an MVC project type, which is still a web application by using these in the PropertyGroup of the .csproj file

<ProjectGuid>{B99EC98A-1F09-4245-B00D-5AF985190AA9}</ProjectGuid>
<ProjectTypeGuids>{603c0e0b-db56-11dc-be95-000d561079b0};{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>

Another opinion I found on StackOverflow.com:
As long as you setup the routing in web.config, setup the necessary directory structure, and add the correct routes in global.asax, you could theoretically add MVC items to any web project. So far as I know, those are the only requirements for it to work.
However, the combination of the two might be a bit confusing and difficult to maintain, long term. Maybe you could move all of the existing web forms site content into a subfolder to keep it out of the way and keep the root directory of the site clean to reduce the clutter and make things more clear.
Finally, the one recommended by Microsoft:

The Microsoft .NET 4.0 exam on Web development (70-519) has almost this exact question in the prep materials. The answer, according to Microsoft then, is:

  1.     Convert the web forms website into a web application (i.e. webapp project).
  2.     Add references to “the ASP.NET MVC 2 assemblies” in the webapp’s config file.

References:

Questions containing convert “asp.net to asp.net mvc” – Stack Overflow

Convert website from webforms to asp.net mvc

Setting up a minimal ASP.NET MVC project in Visual Studio

How would you sprinkle-in ASP.Net MVC into an existing Web Site Project? [closed]

One response to this post.

  1. I almost never leave responses, but i did a few searching and wound up here Convert
    ASP.Net webform to ASP.Net MVC | Nathan. And I do have 2 questions for you if you don’t mind. Could it be simply me or does it give the impression like some of the remarks come across as if they are written by brain dead visitors?😛 And, if you are posting at other social sites, I’d like
    to keep up with anything new you have to post. Could you make a list of the complete urls of your public sites like your twitter feed, Facebook
    page or linkedin profile?

    Responder

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s

A %d blogueros les gusta esto: