.NET MVC 自定义路由

发布于 2024-08-09 18:36:10 字数 143 浏览 6 评论 0原文

我想知道是否可以创建一个比控制器更高级别的路由映射。典型的路由将包括“/controller/action/id”。我正在寻找类似“section/controller/action/id”或“controller/section/action/id”的内容。我该怎么做?

I was wondering if I could create a routing map with one more higher level than the controller. The typical routing would include "/controller/action/id". What I am looking for is something like "section/controller/action/id" or "controller/section/action/id". How can i do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

小傻瓜 2024-08-16 18:36:10

没问题。只需创建一个路由,其 URL 例如

path/to/my/application/{controller}/{action}/{id}

...并像往常一样提供默认控制器和操作。

一个具体的例子是

context.MapRoute(
    "Admin_default",
    "admin/{controller}/{action}/{id}",
    new { controller = "AdminHome", action = "Index", id = "" }
);

这将映射例如以下 URL:

/admin/                   => AdminHomeController.Index
/admin/adminhome/         => AdminHomeController.Index
/admin/other/             => OtherController.Index
/admin/statistics/view/50 => StatisticsController.View(50)

但请注意,如果您还有默认路由,例如如下所示:

context.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = "" }
);

...那么管理路由中的控制器操作方法也可以访问通过这条路线。使用URL路由调试器来确定。

No problem. Just create a route the URL of which is, for example

path/to/my/application/{controller}/{action}/{id}

...and supply a default controller and action as usual.

A concrete example of this is

context.MapRoute(
    "Admin_default",
    "admin/{controller}/{action}/{id}",
    new { controller = "AdminHome", action = "Index", id = "" }
);

This will map, for example, the following URLs:

/admin/                   => AdminHomeController.Index
/admin/adminhome/         => AdminHomeController.Index
/admin/other/             => OtherController.Index
/admin/statistics/view/50 => StatisticsController.View(50)

Note, though, that if you also have a default route, for example like this:

context.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = "" }
);

...then controller action methods in the Admin routing may also be accessible via this route. Use the URL Routing Debugger to find out for sure.

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