超级简单的MVC站点,带有一个区域来处理移动设备。我所有的Area路由都工作得很好,除了一个需要参数的视图。
在"正常"站点,我有一个视图视频页面,需要一个参数。
mysite.com/Video/123456
这工作完美。在我的领域为移动内容做了一点斗争之后,我甚至在控制器和视图中使用了完全相同的代码/标记。所以我希望下面的URL:
mysite.com/Mobile/Video/123456
可以正确解析。它不是。我得到一个404(未找到)。如果我把参数去掉:
mysite.com/Mobile/Video
解析正确
我确信这一定是我在路由中做错了什么。下面是我的global.asax中的适当部分。如有任何帮助,不胜感激。
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Video", // Route name
"Video/{id}", // URL with parameters
new { controller = "Video", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "mysite.Controllers.VideoController" }
);
routes.MapRoute(
"NewsItem", // Route name
"NewsItem/{id}", // URL with parameters
new { controller = "NewsItem", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "mysite.Controllers.HomeController" }
);
routes.MapRoute(
"Mobile", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { area = "Mobile", controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "mysite.Areas.Mobile.Controllers.HomeController" }
);
routes.MapRoute(
"Mobile/Video", // Route name
"Mobile/Video/{id}", // URL with parameters
new { area = "Mobile", controller = "Video", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "mysite.Areas.Mobile.Controllers.VideoController" }
);
}
SteveInTN,您不能在两个Global中拥有相同的注册。asax和mobileareareregistration .cs.
你只需要在mobileareregistration .cs中有移动注册,并在RegisterRoutes(RouteTable.Routes)之前调用Application_Start中的areareregistration . registerallareas()。
如果您想要url如mysite.com/Mobile/Video/123456:移动路由的注册格式应该是{controller}/{id},就像视频路由一样。
Registration in Global.asax:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Video", // Route name
"Video/{id}", // URL with parameters
new { controller = "Video", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "mysite.Controllers.VideoController" }
);
//newsitem route
}
Registration on mobileareregistration:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Mobile_default",
"Mobile/{controller}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
看起来你的路由名称不应该包含/,因为它可能与路由冲突?当我进行路由时,我确保名称是唯一的,并使用下划线来表示分隔符,如下所示: