【发布时间】:2011-11-03 19:46:58
【问题描述】:
超级简单的 MVC 站点,带有一个处理移动设备的区域。除了需要参数的视图之外,我所有的区域路由都可以正常工作。
在“正常”站点中,我有一个需要参数的观看视频页面。
mysite.com/Video/123456
这非常有效。在我的区域为移动内容进行了一些斗争之后,我什至开始在我的控制器和视图中使用完全相同的代码/标记。所以我希望以下网址:
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" }
);
}
【问题讨论】:
-
您正在尝试在 global.asax 中注册区域?
-
是的,上面的代码 sn-p 来自我的 global.asax。我还在我的 MobileAreaRegistration.cs 文件中注册了一条路线。
-
context.MapRoute("Mobile_default", "Mobile/{controller}/{action}/{id}", new { action = "Index", id = UrlParameter.Optional } );跨度>
标签: asp.net-mvc-3 asp.net-mvc-3-areas