简体   繁体   English

如何使用ASP.Net MVC URL路由为所有操作添加前缀?

[英]How to add a prefix to all actions with ASP.Net MVC URL Routing?

I'm trying to write a MapRoute call that will make any route that is prefixed with "json/" prepend "json" to the action's name. 我正在尝试编写一个MapRoute调用,它将使任何以“json /”为前缀的路径将“json”添加到操作的名称之前。 For instance, a route something like this: 例如,这样的路线:

"json/{controller}/{action}"

with "json/Foo/Bar", it should result in: 使用“json / Foo / Bar”,它应该导致:

controller = "Foo"
action = "jsonBar"

Any ideas? 有任何想法吗?

I wonder if it wouldn't be better to include json in the route-data and look it up in the action? 我想知道在路线数据中包含json并在行动中查找它会不会更好? ie when mapping your route, use something like (for the defaults): 即,在映射您的路线时,请使用类似的(默认值):

new { mode="json", controller = "Home", action = "Index", id = "" }

or map the route as: 或将路线映射为:

"{mode}/{controller}/{action}"

then access this in the controller: 然后在控制器中访问它:

string mode = (string) RouteData.Values["mode"];

(or pass it in as an argument) (或作为参数传递)

Other than that, you could potentially write your own route-handler, but that is a lot of work. 除此之外,您可以编写自己的路由处理程序,但这是很多工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM