如何将所有内容重定向到单个控制器?
我有三个特定的路线:
routes.MapRoute(
"Home Page",
"",
new { controller = "Home", action = "Index" }
);
routes.MapRoute(
"Admin Section",
"AdminSection/{action}/{id}",
new { controller = "AdminSection", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Listings",
"{controller}/{action}/{id}",
new { controller = "Listings", action = "Index", id = UrlParameter.Optional }
);
基本上,前两个路线按计划工作,但是,我希望将路线中没有明确的所有内容重定向到 listings
控制器。
我对路由仍然很陌生,并且在过去的一个小时里一直在尝试谷歌搜索,但没有任何运气 - 我确切地知道这里发生了什么,但是,我不知道如何修复它。
我已经使用了 RouteDebugger,我可以看到它正在命中路线,但是,问题是,如果未指定控制器,它只会转到 Listings
控制器 - 但是,显然总会有在那里有什么东西。
我尝试了几种不同的组合 - 我认为通过删除 URL 的 {controller}
部分并仍然定义默认值,我会取得胜利,但是,我运气不佳。
有谁知道我需要做什么?
I have three specific routes:
routes.MapRoute(
"Home Page",
"",
new { controller = "Home", action = "Index" }
);
routes.MapRoute(
"Admin Section",
"AdminSection/{action}/{id}",
new { controller = "AdminSection", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Listings",
"{controller}/{action}/{id}",
new { controller = "Listings", action = "Index", id = UrlParameter.Optional }
);
Basically, the first two routes work as planned, however, I want everything that isn't specifically in a route to be redirected to the listings
controller.
I am still quite new to routing and have been trying to Google this for the past hour without any luck - I know exactly what is going on here, but, I don't know how to fix it.
I have used RouteDebugger, and I can see that it is hitting the route, but, the issue is that it will only go to the Listings
controller if a controller is not specified - but, obviously there will always be something there.
I have tried a few different combinations - I thought I was on to a winner by removing the {controller}
part of the URL and still defining the default value, but, I am not having much luck.
Does anyone know what I need to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
怎么样:
site.com/test :
它将转到
action: test
,controller:listing
,id = Blank
How about this:
site.com/test :
It'll go to
action: test
,controller: listing
,id = blank
编辑:据我了解,您想要一条包罗万象的路线。
http://richarddingwall.name/2008/ 08/09/two-common-aspnet-mvc-url-routing-issues/
原文:
我目前无法测试这个,但这
应该可以。
在您的列表控制器中,只需接受一个字符串参数“anythingOtherThanController”,它将与其绑定。
这里的主要问题是 /some/action 将被映射到与 /another/action 相同的操作。所以我不确定你想在这里做什么:)
Edit: As I understand it you want a catch-all route.
http://richarddingwall.name/2008/08/09/three-common-aspnet-mvc-url-routing-issues/
Original:
I can't test this at the moment but
This should work.
In your Listings controller, just accept a string parameter "anythingOtherThanController" and it will get bound to it.
The main problem here is that /some/action will be mapped to the same action as /another/action. So I'm not sure what you're trying to do here :)
提供默认路由并提供控制器名称作为列表控制器。将此路由映射保留在所有映射的底部。
抱歉,我把顺序搞混了。
Provide a default route and provide controller name as listings controller. Keep this route mapping at the bottom of all the mappings.
Sorry I got sequence mixed.