如何分隔路径相同但功能不同的路线? [英] How do I separate routes that have the same path but do different things?

查看:36
本文介绍了如何分隔路径相同但功能不同的路线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下路由 Page/View/Id 将转到页面控制器"中的 View 方法.我还想要以下路线:

The following route Page/View/Id would go to the View Method in the Page Controller. I'd also like the following route:

/{page-title}

转到相同的方法.这样一来,我可以拥有类似以下的网址:

to go to the same method. That's so I could have URLs like the following:

http://www.mysite.com/This-Is-a-Page

考虑 This-Is-a-Page 也可能是控制器,我该如何配置呢?

How do I configure this, considering This-Is-a-Page might be a controller also?

推荐答案

如果您的控制器"路由和页面"路由(请参见下文)都使用相同的/something ,那么您必须执行以下规则:

If both your "controller" routes and "page" routes (see below) use the same /something, then you're going to have to implement the following rules:

在路线顶部:

route. MapRoute(
    "ControllerRoute"
    "{controller}",
    new { controller = "Home", action = "Index" }
    new { controller = GetControllerNameRegex() }
);

route.MapRoute(
    "PageRoute",
    "{pageSlug}"
    new { controller = "Page", action = "ShowPage" }
);

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆