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

删除回忆录丶 提交于 2021-02-07 06:48:21

问题


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}"

with "json/Foo/Bar", it should result in:

controller = "Foo"
action = "jsonBar"

Any ideas?


回答1:


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 = "" }

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.



来源:https://stackoverflow.com/questions/833996/how-to-add-a-prefix-to-all-actions-with-asp-net-mvc-url-routing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!