如何将所有内容重定向到单个控制器?

发布于 2024-12-06 05:44:55 字数 886 浏览 0 评论 0原文

我有三个特定的路线:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风流物 2024-12-13 05:44:55

怎么样:

routes.MapRoute("Listings", "{action}/{id}", 
        new { controller = "Listings", action = "Index", id = UrlParameter.Optional });

site.com/test :

它将转到 action: test, controller:listing, id = Blank

在此处输入图像描述

How about this:

routes.MapRoute("Listings", "{action}/{id}", 
        new { controller = "Listings", action = "Index", id = UrlParameter.Optional });

site.com/test :

It'll go to action: test, controller: listing, id = blank

enter image description here

浮生面具三千个 2024-12-13 05:44:55

编辑:据我了解,您想要一条包罗万象的路线。

http://richarddingwall.name/2008/ 08/09/two-common-aspnet-mvc-url-routing-issues/

routes.MapRoute("Listings", "{*url}",
    new { controller = "Listings", action = "Index" }
);

原文:

我目前无法测试这个,但这

routes.MapRoute(
   "Listings",
   "{anythingOtherThanController}/{action}/{id}",
   new { controller = "Listings", action = "Index", id = UrlParameter.Optional }
   );

应该可以。

在您的列表控制器中,只需接受一个字符串参数“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/

routes.MapRoute("Listings", "{*url}",
    new { controller = "Listings", action = "Index" }
);

Original:

I can't test this at the moment but

routes.MapRoute(
   "Listings",
   "{anythingOtherThanController}/{action}/{id}",
   new { controller = "Listings", action = "Index", id = UrlParameter.Optional }
   );

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 :)

岁月苍老的讽刺 2024-12-13 05:44:55

提供默认路由并提供控制器名称作为列表控制器。将此路由映射保留在所有映射的底部。

 routes.MapRoute(
   "Default",
   "{controller}/{action}/{id}",
   new { controller = "Listings", action = "Index", id = UrlParameter.Optional }
   );

抱歉,我把顺序搞混了。

Provide a default route and provide controller name as listings controller. Keep this route mapping at the bottom of all the mappings.

 routes.MapRoute(
   "Default",
   "{controller}/{action}/{id}",
   new { controller = "Listings", action = "Index", id = UrlParameter.Optional }
   );

Sorry I got sequence mixed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文