ASP.NET MVC 路由的高级路由行为

发布于 2024-08-09 03:03:31 字数 1219 浏览 5 评论 0原文

给定一个遵循以下模式的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半寸时光 2024-08-16 03:03:31

首先,如果您要使用 key=value 格式,那么我建议使用 QueryString 而不是 URL。

但如果没有,您可以这样做:

//register this route
routes.MapRoute("color", "colors/processcolors/{*q}",
    new { controller = "Color", action ="ProcessColors" });

然后在您的 ColorController 中:

public ActionResult ProcessColors(string q) {
    string[] colors = GetColors(q);
    return View();
}

private string[] GetColors(string q) {
    if (String.IsNullOrEmpty(q)) {
        return null;
    }
    return q.Split("/".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
}

在这种情况下,您的 URL 将如下所示:

site.com/colors/processcolors/red
site.com/colors/processcolors/red/green

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 :

//register this route
routes.MapRoute("color", "colors/processcolors/{*q}",
    new { controller = "Color", action ="ProcessColors" });

Then in your ColorController :

public ActionResult ProcessColors(string q) {
    string[] colors = GetColors(q);
    return View();
}

private string[] GetColors(string q) {
    if (String.IsNullOrEmpty(q)) {
        return null;
    }
    return q.Split("/".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
}

In this case your URLs will be like this :

site.com/colors/processcolors/red
site.com/colors/processcolors/red/green
木槿暧夏七纪年 2024-08-16 03:03:31

在我们使用通配符映射的情况下,我想我们失去了使用 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文