ASP.NET MVC 路由的高级路由行为
给定一个遵循以下模式的 url:
firstcolor={value1}/secondcolor={value2},
其中 value1 和 value2 可以变化,并且操作方法如下:
ProcessColors(string color1, string color2),位于名为 ColorController 的控制器中。
我想要以下路由评估:
URL '/firstcolor=red' 导致类似 ProcessColors("red", null) 的调用
URL“/secondcolor=blue”会导致类似 ProcessColors(null, "blue") 的调用
URL 'firstcolor=red/secondcolor=blue' 最终以 ProcessColors("red", "blue") 之类的调用结束,
现在我认为这可以通过一些路线来实现,就像这样
route.MapRoute(null,
"firstcolor={color1}/secondcolor={color2}",
new { controller=ColorController, action = ProcessColors })
route.MapRoute(null,
"firstcolor={color1}}",
new { controller=ColorController, action = ProcessColors, color2 = (string)null })
route.MapRoute(null,
"secondcolor={color2}}",
new { controller=ColorController, action = ProcessColors, color1 = (string)null })
这对于 2 种颜色来说就足够了,但据我所知,如果我们想要有 4 种颜色并且能够拥有如下 URL:
'/firstcolor=blue/secondcolor=red/thirdcolor=green/fourthcolor, 我们最终会得到大量的路由=黑色'
'/firstcolor=blue/thirdcolor=red'
'/thirdcolour=red/fourthcolour=black'
等等,即我们需要满足任何组合,因为第一个颜色总是在第二个颜色之前,第二个颜色总是在第三个颜色之前,依此类推。
忽略我的荒谬的例子,有没有什么好的方法可以处理这种不涉及需要创建大量路线和操作方法的情况?
Given a url that follows the following pattern:
firstcolor={value1}/secondcolor={value2}
where value1 and value2 can vary and an action method like:
ProcessColors(string color1, string color2) in say a controller called ColorController.
I want the following route evaluation:
URL '/firstcolor=red' results in a call like ProcessColors("red", null)
URL '/secondcolor=blue'results in a call like ProcessColors(null, "blue")
URL 'firstcolor=red/secondcolor=blue' ends up in a call like ProcessColors("red", "blue")
Now from I think this can be achieved with a few routes, something like this
route.MapRoute(null,
"firstcolor={color1}/secondcolor={color2}",
new { controller=ColorController, action = ProcessColors })
route.MapRoute(null,
"firstcolor={color1}}",
new { controller=ColorController, action = ProcessColors, color2 = (string)null })
route.MapRoute(null,
"secondcolor={color2}}",
new { controller=ColorController, action = ProcessColors, color1 = (string)null })
This is sufficient for just 2 colors, but as far as I can tell we'll end up with a proliferation of routes if we wanted to have, say 4 colors and be able to have URL's like this:
'/firstcolor=blue/secondcolor=red/thirdcolor=green/fourthcolor=black'
'/firstcolor=blue/thirdcolour=red'
'/thirdcolour=red/fourthcolour=black'
and so on, i.e. we need to cater for any combination given that firstcolor will always be before 2nd, 2nd will always be before 3rd and so on.
Ignoring my ridiculous example, is there any nice way to deal with this sort of situation that doesn't involve lots of routes and action methods needing to be created?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,如果您要使用
key=value
格式,那么我建议使用 QueryString 而不是 URL。但如果没有,您可以这样做:
然后在您的
ColorController
中:在这种情况下,您的 URL 将如下所示:
First of all, if you are going to use that
key=value
format, then I suggest using QueryString instead of the URL.But if not, you can do this :
Then in your
ColorController
:In this case your URLs will be like this :
在我们使用通配符映射的情况下,我想我们失去了使用 Html.ActionLink 为我们构建 URL 的能力?
In the case that we use the wildcard mapping I suppose we lose the ability to use Html.ActionLink to build our URL's for us?