如何设置路由以捕获控制器后的所有后续URL段作为参数? [英] How do I setup a route to capture all subsequent URL segments after the controller as a parameter?
问题描述
我想设置以下形式的路由(例如在HTTP处理程序中)
I want to setup a route of the following form (like in an HTTP handler)
控制器/*/*/*/*(任意深度)
Controller/ * / * / * /* (to any arbitrary depth)
我是MVC路线的新手,但我想设置一条基本上像这样的路线
I'm new to MVC routes but I want to setup a route where basically it's like
MyController/{UrlSegments}
MyController/{UrlSegments}
例如,例如MyController/assets/images/logo.png将转到MyController并将"assets/images/logo.png"作为参数传递给路由
So for instance MyController/assets/images/logo.png would goto MyController and pass "assets/images/logo.png" as a parameter to the route
推荐答案
如果您这样设置路由映射:
If you set up your route mapping like this:
routes.MapRoute(
name: "Default",
url: "{controller}/{*stuff}",
defaults: new { controller = "Default", action = "DefaultAction" }
);
然后当您点击
/Default/one/two/three/four
它将在DefaultController上命中DefaultAction,并且您将需要一个名为stuff的字符串参数,其值将为
It will hit the DefaultAction on the DefaultController and you will need a string parameter called stuff that will have a value of
one/two/three/four
这篇关于如何设置路由以捕获控制器后的所有后续URL段作为参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!