如何使用ASP.Net MVC URL路由为所有操作添加前缀? [英] How to add a prefix to all actions with ASP.Net MVC URL Routing?
问题描述
我正在尝试编写一个MapRoute调用,该调用将使任何以"json/"为前缀的路由都以"json"作为该动作的名称.例如,一条类似这样的路线:
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. For instance, a route something like this:
"json/{controller}/{action}"
带有"json/Foo/Bar",结果应为:
with "json/Foo/Bar", it should result in:
controller = "Foo"
action = "jsonBar"
有什么想法吗?
推荐答案
我想知道将json包含在路由数据中并在操作中进行查找是否更好?也就是说,在映射路线时,请使用类似(用于默认设置)的方法:
I wonder if it wouldn't be better to include json in the route-data and look it up in the action? i.e. when mapping your route, use something like (for the defaults):
new { mode="json", controller = "Home", action = "Index", id = "" }
或将路线映射为:
"{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.
这篇关于如何使用ASP.Net MVC URL路由为所有操作添加前缀?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!