Connect an ASP.NET MVC project to an ASP.NET WebForms project

Why might this be needed? Suppose we have a large ASP.NET WebForms application and have a project written in ASP.NET MVC and we want to combine them.

Let's say the address of our site is: and we want MVC to be available at

Preconditions: we have a solution with two projects - ASP.NET Web Application (WebFormsApplication) and ASP.NET MVC project (MvcApplication).

What should be done.
1. Install ASP.NET MVC for WebFormsApplication
2. Add a link to WebFormsApplication on MvcApplication
3. Add a public method to MvcApplication similar to Application_Start
4. Call this method in Application_Start of the WebFormsApplication application
5. Using Razor Generator, include Views in the MvcApplication assembly
4. ...
5. Profit !!!

1. Install ASP.NET MVC for WebFormsApplication, done via nuget with the following command: Get-Project WebFormsApplication | Install-Package AspNetMvc

2. Add a link in WebFormsApplication to MvcApplication. It's simple - Add Reference ... then you know

3. Add a public method to MvcApplication similar to Application_Start
Global.asax MvcApplication code
public class MvcApplication : System.Web.HttpApplication
        protected void Application_Start()
        public static void Start(string prefix = null)
            // let's register routs with prefix
            RouteConfig.RegisterRoutes(RouteTable.Routes, prefix);

Here there is such a parameter as “prefix”, it is needed for routing. Accordingly, we will modify the RegisterRoutes method of the RouteConfig class so that it can accept this parameter.

RouteConfig MvcApplication Code
public class RouteConfig
        public static void RegisterRoutes(RouteCollection routes, string prefix)
            if (!string.IsNullOrEmpty(prefix) && !prefix.EndsWith("/"))
                prefix += "/";
                name: "Default",
                url: prefix + "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }

4. Add a call to this method in the Application_Start application WebFormsApplication
Application_Start code Global.asax WebFormsApplication
void Application_Start(object sender, EventArgs e)
            // Code that runs on application startup
            // Here we add MVC app

5. Install the plug-in for Visual Studio.
To do this, install Razor Generator through Extensions and Updates (do not confuse it with Razor Single File Generator for MVC)

6. Using Razor Generator, enable View in the MvcApplication assembly
Install Razor Generator for the MvcApplication project, do this through nuget with the next Get-Project MvcApplication command | Install-Package RazorGenerator.Mvc
Now for the required View, set the properties Build Action: None, Custom Tool: RazorGenerator. After that, files with the name viewName.generated.cs will be generated for View

. That's all. Now you can build projects and see what happened.

Here are some
Razor Generator
related links Precompile your MVC Razor views using RazorGenerator
ASP.NET MVC - how to use it with WebForms
ASP.NET MVC - how to take part of an application into a separate assembly?
ASP.NET WebForms and ASP.NET MVC in Harmony
Add MVC to your Web Forms project

Also popular now: