📜 ⬆️ ⬇️

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

Why this may be needed? Suppose we have a great ASP.NET WebForms application and there is a project written in ASP.NET MVC and we want to integrate them.

Suppose our site address is: www.my-awesome-site.com and we want MVC to be available at www.my-awesome-site.com/mvc-stuff

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 the Application_Start of the WebFormsApplication application
5. Use the Razor Generator to include Views in the MvcApplication assembly
four.…
5. Profit !!!



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

2. Add a link to the WebFormsApplication on MvcApplication. Everything is 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() { Start(); } public static void Start(string prefix = null) { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); // let's register routs with prefix RouteConfig.RegisterRoutes(RouteTable.Routes, prefix); BundleConfig.RegisterBundles(BundleTable.Bundles); AuthConfig.RegisterAuth(); } } 


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

MvcApplication RouteConfig Code
 public class RouteConfig { public static void RegisterRoutes(RouteCollection routes, string prefix) { if (!string.IsNullOrEmpty(prefix) && !prefix.EndsWith("/")) { prefix += "/"; } routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( 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
Code Application_Start Global.asax WebFormsApplication
 void Application_Start(object sender, EventArgs e) { // Code that runs on application startup BundleConfig.RegisterBundles(BundleTable.Bundles); AuthConfig.RegisterOpenAuth(); // Here we add MVC app MvcApplication.MvcApplication.Start("mvc-stuff"); } 


5. Install the plugin for Visual Studio
To do this, you need to install the Razor Generator via Extensions and Updates (not to be confused with the Razor Single File Generator for MVC)

6. Using the Razor Generator, we include the View in the MvcApplication assembly
Install the Razor Generator for the MvcApplication project, this is done via nuget with the following command Get-Project MvcApplication | Install-Package RazorGenerator.Mvc
Now for the required View, set the properties Build Action: None, Custom Tool: RazorGenerator. Then for the View files with the name viewName.generated.cs will be generated.

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

Here are a few related links.
Razor generator
Precompile your MVC Razor views using RazorGenerator
ASP.NET MVC - How to Use with WebForms
ASP.NET MVC - how to move part of an application to a separate assembly?
ASP.NET WebForms and ASP.NET MVC in Harmony
Add MVC to Web Forms Project

Source: https://habr.com/ru/post/153387/


All Articles