您可以在 Web 应用程序的子文件夹中创建 MVC 应用程序,然后将任何 Global.asax 配置移动到您的 WebForms 应用程序
您只需要更新您的路由,如果您的 MVC 应用程序是在名为 MVC 的文件夹中创建的,它可能看起来像这样:
routes.MapRoute(
"MVC default",
"MVC/default.aspx",
new { controller = "Home", action = "Index", id = "" }
);
routes.MapRoute(
"Services",
"MVC/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
编辑:
实际上,您需要映射以确保您的视图也正确映射,您可以通过创建快速自定义视图引擎来做到这一点:
public class CustomViewEngine : System.Web.Mvc.WebFormViewEngine
{
public CustomViewEngine()
{
base.ViewLocationFormats = new string[]
{
"~/MVC/Views/{1}/{0}.aspx",
"~/MVC/Views/{1}/{0}.ascx",
"~/MVC/Views/Shared/{0}.aspx",
"~/MVC/Views/Shared/{0}.ascx"
};
base.PartialViewLocationFormats = new string[] {
"~/MVC/Views/{1}/{0}.aspx",
"~/MVC/Views/{1}/{0}.ascx",
"~/MVC/Views/Shared/{0}.aspx",
"~/MVC/Views/Shared/{0}.ascx"
};
base.MasterLocationFormats = new string[]
{
"~/MVC/Views/{1}/{0}.master",
"~/MVC/Views/Shared/{0}.master"
};
}
}
然后在你的 global.asax 中注册它:
System.Web.Mvc.ViewEngines.Engines.Clear();
System.Web.Mvc.ViewEngines.Engines.Add(new CustomViewEngine());