ASP.NET MVC 路由和查询字符串

发布于 2024-11-09 03:03:19 字数 533 浏览 0 评论 0原文

有人可以告诉我为什么,像这样的网址...

http://localhost:22220/groups/go/1234/2525?name=Bob

这个路由映射不匹配...

routes.MapRoute(null, // Route name
                "groups/go/{groupId}/{userId}/{name}",
                new { controller = "Groups", action = "Go" });

但是这个路由映射似乎匹配? (使用 Phil Haack 的路由测试器,这是“生成的 URL”)...

context.MapRoute("Teams_Default",
                 "Teams/{controller}/{action}/{id}",
                 new { id = UrlParameter.Optional });

Can someone please tell me why, with a url like this...

http://localhost:22220/groups/go/1234/2525?name=Bob

This route mapping doesn't match...

routes.MapRoute(null, // Route name
                "groups/go/{groupId}/{userId}/{name}",
                new { controller = "Groups", action = "Go" });

But this route mapping appears to match? (Using Phil Haack's Route Tester, this is the 'Generated URL')...

context.MapRoute("Teams_Default",
                 "Teams/{controller}/{action}/{id}",
                 new { id = UrlParameter.Optional });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

温柔嚣张 2024-11-16 03:03:19

该链接必须是:
http://localhost:22220/groups/go/1234/2525/Bob

或者您可以将路线更改为
“组/去/{groupId}/{userId}”

The link needs to be :
http://localhost:22220/groups/go/1234/2525/Bob

Or you could change the route to
"groups/go/{groupId}/{userId}"

清浅ˋ旧时光 2024-11-16 03:03:19

由于该 URL 上最后一个“name”参数的格式不正确,因此第一个路由不会匹配。如果将路由更改为:

routes.MapRoute(null, // Route name
                "groups/go/{groupId}/{userId}/{name}",
                new { controller = "Groups", action = "Go", name = "Bob" });

由于“名称”的默认值,它将起作用。

显然,这对您没有好处,因为您希望读取该名称。

我认为更大的问题是:该 URL 是如何生成的?

Since the last 'name' parameter is not formed correctly on that URL, the first route doesn't get matched. If you change the route to this:

routes.MapRoute(null, // Route name
                "groups/go/{groupId}/{userId}/{name}",
                new { controller = "Groups", action = "Go", name = "Bob" });

it will work because of the default value for 'name'.

Obviously, this is no good for you since you want the name to be read.

I think the bigger question is: How is that URL being generated?

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