CodeDigest.Com Logo

Adding Asp.Net WebForms into Asp.Net MVC Project and Vice Versa

Tagged as: Asp.Net Asp.Net MVC Posted By

There are situations where you need to mix Asp.Net WebForms into a MVC project or vice versa. For instance, you would have had a project running on Asp.Net WebForms all these days and you want to develop any new features using MVC instead of WebForms. Due to resource crunch or lack of time, you may not migrate the entire WebForms into MVC or sometimes, a mature and production tested application are hard to replace when we know that WebForms technology are here to stay and won’t get retired.

If you have your project already on any of the latest version of Visual Studio (2013/2015), the task is very easy. i.e. you can add a new controller class into your WebForms project through “Add new item..” dialog and it will start working. This is because, the latest versions of Asp.Net and Visual studio project templates has all the required components to run Asp.Net MVC included by default even if it is a WebForms project. On the other hand, if you have your WebForms project developed in a bit older Visual Studio like 2010, you may need to tweak a bit to add the required MVC assemblies, configuration settings and folders structures for Controllers and Views that are required by convention.  The vice versa is very easy, just adding a new WebForm into a MVC project will just work fine without any changes. Before seeing this in detail, let’s understand some of basics of Asp.Net MVC and WebForms runtime request processing in detail.

Asp.Net Request Processing

Both Asp.Net WebForms and Asp.Net MVC shares a common runtime and in specific, the request processing pipeline are also shared by both. When there is web request made to the application, the Asp.Net Routing Module will check if the incoming url matches to any of its registered routes, if it is then it is processed by Asp.Net MVC Handler else the request will be processed by a Asp.Net Page HttpHandler. All the core Asp.Net objects like Session, Cache, Request, Response object will be accessible by both the application code.

Adding WebForms into Asp.Net MVC project

Let’s assume you are developing a new Asp.Net MVC project and you would like to bring some of the WebForms module from an existing project into this. It is as simple as just including those WebForms and dependent BL/DL code into your project will make it work. This is because, as I said before both shares same runtime and request processing pipeline which will forward the request to aspx page to an Asp.Net Page Handler without any configuration changes. Let’s examine the default routing settings which is making this happens now,


 public static void RegisterRoutes(RouteCollection routes)




                "Default", // Route name

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

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





Clearly, there are no routes/settings that will process the incoming aspx request but it still works. This is because of the property RouteCollection.RouteExistingFiles which is set to false by default. This means that the routing module will ignore all the request to a physical file in your project and leave it to the Asp.Net pipeline to process it. When you set this property to true, your application will throw "HTTP 404 Not found" error since your aspx page now has no route mapping on the routing table. If you decide to go with setting this property to true for some reasons, then you need to make some additional settings for the MVC routing module to forward the aspx request to Asp.Net Handler.

Routing Changes to Resolve Webform Page


public static void RegisterRoutes(RouteCollection routes)


            routes.RouteExistingFiles = true;           









                "Default", // Route name

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

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





The above route configuration will dictate the MVC routing module to ignore request for aspx, ashx extensions and files inside Content and Script folders (client-side stuffs). When routing modules ignores, it will be processed by the default Asp.Net pipeline as usual and the page executes.


Adding Asp.Net MVC Views into Asp.Net WebForms Project

As I said before, if you have your project in older IDE(like 2010) then it is bit tricky, you need to include the MVC assemblies and required MVC folder structure by convention into you project yourselves.

First let us inlcude the folder structure, you can either manually create the folder structure or the easiest way is to create a new MVC project using Visual Studio and copy the Views, Controllers, Script, Contents folder into your WebForms project. Next, you need to include the following dlls at the minimum to run your basic MVC controller, action, and razor based view.

  1. System.Web.Mvc

  2. System.Web.Abstractions

  3. System.Web.Routing

  4. System.Web.WebPages

Note - The System.Web.Routing dll can be added for .NetFramework 4.0(This packages is also used in Asp.Net request pipeline for routing, so version 4.0 dlls will do). Similarly, you can include System.Web.Abstractions for version 4.0.

You need to add the following configuration settings into your root web.config file.



    <compilation debug="true" targetFramework="4.0">


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

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

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

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

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





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

        <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.Web.WebPages"/>




//Below settings to be copied outside <System.Web> and Inside <configuration>


    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">


        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />

        <bindingRedirect oldVersion="" newVersion="" />






Please make sure you have the web.config file copied from a new MVC project into Views folder.

That’s all required to run your Asp.Net MVC views from your WebForms project.

Note Alternatively, you can also create a new Asp.Net MVC project and include your WebForms and related application code if you are unable to get it to work by including the required assemblies yourself as discussed in Adding WebForms into Asp.Net MVC project section.

Adding MVC Features in Visual Studio 2012, 2013, 2015 WebForms Project

The above approach can be used when your existing project is an older version of Visual Studio like 2010. If your project is already on Visual Studio 2012+, then adding a MVC Controller class itself will automatically download all the required assemblies and configure all the neccessary things to run MVC. To do this, right click you project and click Add> New Scaffolded Item…

This will bring a dialog to choose the Controller class like below.

Click “MVC 5 Controller – Empty”. Visual Studio will download and configure all the items for you to start working on MVC. Also, to guide Visual Studio will present a readme file like below,

Visual Studio has added the full set of dependencies for ASP.NET MVC 5 to project 'WebFormsWithMVC'. 

The Global.asax.cs file in the project may require additional changes to enable ASP.NET MVC.

1. Add the following namespace references:

    using System.Web.Mvc;

    using System.Web.Routing;

    using System.Web.Optimization;


2. If the code does not already define an Application_Start method, add the following method:

    protected void Application_Start()




3. Add the following lines to the end of the Application_Start method:






Note – It will add a new RouteConfig, BundleConfig files (hopefully RouteConfig2.cs and BundleConfig2.css) in App_Start folder. Merge this settings in your existing class and update Global.asax Application_Start code.




Terrible Idea
I'm sure there is a use case for this but integrating MVC into a legacy Webform app is a horrible idea. Then there is a _layout vs. masterpage. Using the Viewbag to deliver data or partial pages is awful. This article is making life difficult for people with bosses who think this will make life easier. Work smarter, create a separate mvc app, not harder by trying to marry these frameworks.
Commented by MyNightmare on 12/8/2020 9:45:44 AM

Can you provide documentation for adding.... ascx
I see the: routes.IgnoreRoute("{WebPage}.aspx/{*pathInfo}"); How do you do it for ascx, the same way?
Commented by MP on 5/18/2018 1:30:59 PM