我有一个 ASP.NET MVC4应用程序,该应用程序在Global.asax.cs中定义了以下路由。应用的起始页是从主控制器的操作方法 Index() 返回的 Index.cshtml 视图。然后,我添加了一个旧版 ASP.NET WebForms 应用程序,该应用程序将 Default.aspx 作为起始页。如何使此默认.aspx页面作为此集成的 MVC+WebForms 应用程序的起始页:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"{controller}.mvc/{action}/{id}",
new { action = "Index", id = "" }
);
routes.MapRoute(
"Root",
"",
new { controller = "Home", action = "Index", id = "" }
);
}
在 Global.asax 中尝试以下操作:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Web Forms default
routes.MapPageRoute(
"WebFormDefault",
"",
"~/default.aspx"
);
// MVC default
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // parameter default
);
}
另外,我认为您不需要Default
路线的.mvc
部分。
从默认值参数中删除"控制器="主页"对我来说非常有用。我在这里找到了解决方案:https://stackoverflow.com/a/21213711/9793076。
将默认参数从:
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
自:
new { action = "Index", id = UrlParameter.Optional }
如@nam所说,当前的解决方案中断了MVC链路的路由。