路由 - 区域控制器/视图与参数 [英] Routing - Area Controller/View with parameter
问题描述
超级简单的MVC站点的区域来处理移动设备。我所有的区域路由的正常工作与期望一个参数视图的除外。
在正常的网站,我有一个期望参数的查看视频页面。
mysite.com/Video/123456
这完美的作品。在我区的手机内容打这场对位后我甚至有所下降在我的控制器和视图使用完全相同的code /标记。所以,我期望以下网址:
mysite.com/Mobile/Video/123456
会妥善解决。它没有。我得到一个404(未找到)。如果我把参数关闭:
mysite.com/Mobile/Video
它解决了正常。
我相信这一定有什么东西我做错了路由。下面是从我的Global.asax中相应的部分。任何帮助将是AP preciated。
公共静态无效的RegisterRoutes(RouteCollection路线)
{
routes.IgnoreRoute({}资源个.axd / {*} PATHINFO); routes.MapRoute(
视频,//路线名称
视频/(编号),// URL带参数
新{控制器=视频,行动=索引,ID = UrlParameter.Optional} //参数默认
新的String [] {mysite.Controllers.VideoController}
); routes.MapRoute(
NewsItem,//路线名称
NewsItem /(编号),// URL带参数
新{控制器=NewsItem,行动=索引,ID = UrlParameter.Optional} //参数默认
); routes.MapRoute(
默认,//路线名称
{控制器} / {行动} / {ID},// URL带参数
新{控制器=家,行动=索引,ID = UrlParameter.Optional} //参数默认
新的String [] {mysite.Controllers.HomeController}
); routes.MapRoute(
移动,//路线名称
{控制器} / {行动} / {ID},// URL带参数
新{面积=移动,控制器=家,行动=索引,ID = UrlParameter.Optional} //参数默认
新的String [] {mysite.Areas.Mobile.Controllers.HomeController}
); routes.MapRoute(
移动/视频,//路线名称
移动/视频/(编号),// URL带参数
新{面积=移动,控制器=视频,行动=索引,ID = UrlParameter.Optional} //参数默认
新的String [] {mysite.Areas.Mobile.Controllers.VideoController}
);
}
SteveInTN,你不能有两个相同的注册,Global.asax中和MobileAreaRegistration.cs。
您只需要对MobileAreaRegistration.cs流动登记和之前的RegisterRoutes(RouteTable.Routes)调用AreaRegistration.RegisterAllAreas()中的Application_Start。
如果你想网址,如下mysite.com/Mobile/Video/123456:
移动路线报名,应在格式{控制器} / {ID},如视频路线。
登记在Global.asax中:
公共静态无效的RegisterRoutes(RouteCollection路线)
{
routes.IgnoreRoute({}资源个.axd / {*} PATHINFO); routes.MapRoute(
视频,//路线名称
视频/(编号),// URL带参数
新{控制器=视频,行动=索引,ID = UrlParameter.Optional} //参数默认
新的String [] {mysite.Controllers.VideoController}
);
// newsitem路线
}
在MobileAreaRegistration报名方式:
公共覆盖无效RegisterArea(AreaRegistrationContext上下文)
{
context.MapRoute(
Mobile_default
移动/ {}控制器/(编号),
新{行动=索引,ID = UrlParameter.Optional}
);
}
Super simple MVC site with an Area to handle mobile devices. All of my Area routing works fine with the exception of a view that expects a parameter.
In the "normal" site I have a view video page that expects a parameter.
mysite.com/Video/123456
This works perfectly. After fighting this for a bit in my Area for the mobile content I have even gone down to using the exact same code/markup in my Controller and View. So I would expect that the following URL:
mysite.com/Mobile/Video/123456
Would resolve properly. It doesn't. I get a 404 (not found). If I take the parameter off:
mysite.com/Mobile/Video
It resolves properly.
I am sure this must be something I am doing wrong in the routing. Below is the appropriate section from my global.asax. Any help would be appreciated.
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, you cannot have the same registration in both, Global.asax and MobileAreaRegistration.cs.
You only need to have Mobile Registration on MobileAreaRegistration.cs and call AreaRegistration.RegisterAllAreas() in Application_Start before RegisterRoutes(RouteTable.Routes).
If you want url like mysite.com/Mobile/Video/123456: The mobile route registration should be in the format {controller} / {id}, like video route.
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 MobileAreaRegistration:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Mobile_default",
"Mobile/{controller}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
这篇关于路由 - 区域控制器/视图与参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!