任何方法来创建一个包罗万象的路线 [英] Any way to create a catch all Route

查看:120
本文介绍了任何方法来创建一个包罗万象的路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制器上的许多行动

I have many actions on a controller

Controller1\Action1
Controller1\Action2
Controller1\Action3
Controller1\Action4
Controller1\Action5
Controller1\Action6

我想我的网址永远只是:

I would like my URLs to always be just:

Action1
Action2
Action3
Action4
Action5
Action6

有没有一种简单的方法来做到这一点?我不会有任何冲突,其中:

Is there an easy way to do this? I won't have any conflicts where:

Controller1\Action1
Controller2\Action1

每一个动作都会有一个唯一的名称。

Every action will have a unique name.

推荐答案

您应该能够做这样的事:

You should be able to do something like this:

routes.MapRoute(
    "CatchAll",
    "{action}/{*path}",
    new { controller = "MyController", action = "Index" }
);

基本上你告诉它所有的请求应该将路径作为行动和他们都应该去myController的。行动=指数作为该操作未在URL中提供的事件的缺省值。

Essentially you're telling it all requests should treat the path as the Action and they should all go to "MyController". The action = "Index" acts as a default in the event that an Action is not provided in the Url.

上面的路由定义将评估为真,即使额外的,无效的路径数据动作名称后面。这也就意味着,无论 /措施1 /措施1 / 251958125ad / 2512qsadfa2 将前往措施1()。如果您想preFER只有确切路径被允许的:

The above route definition will evaluate to true even if extra, invalid path data is appended after the action name. What that means is that both /Action1 and /Action1/251958125ad/2512qsadfa2 will go to Action1(). If you would prefer that only the exact path be allowed:

routes.MapRoute(
    "CatchAll",
    "{action}",
    new { controller = "MyController", action = "Index" }
);

这篇关于任何方法来创建一个包罗万象的路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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