ASP.NET 4 RTM 中的可选路由参数不再像以前那样工作
我今天使用 ASP.NET MVC 2.0 RTM
将项目升级到 ASP.NET 4 RTM
。
我之前使用的是 ASP.NET 3.5
和 ASP.NET MVC 2.0 RTM
。
我的一些路线突然不起作用,我不知道为什么。我不确定 3.5 和 4.0 之间是否发生了变化 - 或者这是否是 4.0 RTM 中的回归类型问题。 (我之前从未使用 4.0 测试过我的应用程序)。
我喜欢使用 Url.RouteUrl("route-name", routeParams)
来避免生成 URL 时出现歧义。这是我的画廊页面的路线定义。我希望 imageID
是可选的(如果您不指定它,您会得到一个缩略图页面)。
// gallery id
routes.MapRoute(
"gallery-route",
"gallery/{galleryID}/{imageID}/{title}",
new { controller = "Gallery", action = "Index",
galleryID = (string) null,
imageID = (string) null,
title = (string) null}
);
在 .NET 3.5 / ASP.NET 2.0 RTM / IIS7
Url.RouteUrl("gallery-route", new { galleryID = "cats"})
=> /gallery/cats
Url.RouteUrl("gallery-route", new { galleryID = "cats", imageID = "4")
=> /gallery/cats/4
Url.RouteUrl("gallery-route", new { galleryID = "cats", imageID = "4", title = "tiddles")
=> /gallery/cats/4/tiddles
中 在 .NET 4.0 RTM / ASP.NET 2.0 RTM / IIS7 中
Url.RouteUrl("gallery-route", new { galleryID = "cats"})
=> null
Url.RouteUrl("gallery-route", new { galleryID = "cats", imageID = "4")
=> /gallery/cats/4
Url.RouteUrl("gallery-route", new { galleryID = "cats", imageID = "4", title = "tiddles")
=> /gallery/cats/4/tiddles
以前,我只能提供 galleryID
,而生成的 URL 中的其他所有内容都将被忽略。但现在看来我需要指定 title
之前的所有参数 - 否则它会放弃确定 URL。
传入 URL 对于 /gallery/cats
工作正常,并且通过此规则正确映射,imageID
和 title
均被分配 null< /code> 在我的控制器中。
我还使用 http://haacked.com/ 测试了传入路由archive/2008/03/13/url-routing-debugger.aspx 它们都工作正常。
I upgraded my project to ASP.NET 4 RTM
with ASP.NET MVC 2.0 RTM
today.
I was previously using ASP.NET 3.5
with ASP.NET MVC 2.0 RTM
.
Some of my routes don't work suddenly and I don't know why. I'm not sure if something changed between 3.5 and 4.0 - or if this was a regression type issue in the 4.0 RTM. (I never previously tested my app with 4.0).
I like to use Url.RouteUrl("route-name", routeParams)
to avoid ambiguity when generating URLs. Here's my route definition for a gallery page. I want imageID
to be optional (you get a thumbnail page if you don't specify it).
// gallery id
routes.MapRoute(
"gallery-route",
"gallery/{galleryID}/{imageID}/{title}",
new { controller = "Gallery", action = "Index",
galleryID = (string) null,
imageID = (string) null,
title = (string) null}
);
In .NET 3.5 / ASP.NET 2.0 RTM / IIS7
Url.RouteUrl("gallery-route", new { galleryID = "cats"})
=> /gallery/cats
Url.RouteUrl("gallery-route", new { galleryID = "cats", imageID = "4")
=> /gallery/cats/4
Url.RouteUrl("gallery-route", new { galleryID = "cats", imageID = "4", title = "tiddles")
=> /gallery/cats/4/tiddles
In .NET 4.0 RTM / ASP.NET 2.0 RTM / IIS7
Url.RouteUrl("gallery-route", new { galleryID = "cats"})
=> null
Url.RouteUrl("gallery-route", new { galleryID = "cats", imageID = "4")
=> /gallery/cats/4
Url.RouteUrl("gallery-route", new { galleryID = "cats", imageID = "4", title = "tiddles")
=> /gallery/cats/4/tiddles
Previously I could supply only the galleryID
and everything else would be ignored in the generated URL. But now it's looking like I need to specify all the parameters up until title
- or it gives up in determining the URL.
Incoming URLs work fine for /gallery/cats
and that is correctly mapped through this rule with imageID
and title
both being assigned null
in my controller.
I also tested the INCOMING routes with http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx and they all work fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 ASP.NET MVC 2.0 中指定可选参数的正确方法是使用 UrlParameter.Optional 字段:
假设以下控制器和操作:
所有这些都将按预期工作:
渲染为:
备注:在 Windows 7 x64、Visual Studio 2010 RTM、ASP.NET MVC 2.0 项目上测试。
The correct way of specifying optional parameters in ASP.NET MVC 2.0 is using the UrlParameter.Optional field:
Assuming the following controller and action:
All these will work as expected:
Render as:
Remark: Tested on Windows 7 x64, Visual Studio 2010 RTM, ASP.NET MVC 2.0 project.
我知道问题是针对 MVC2 的,但在 MVC3 中,Darin Dimitrov 给出的答案将失败。
Phil Haack 在他的博客中解释了这个问题并给出了解决方法:
http://haacked .com/archive/2011/02/20/routing-regression-with-two-consecutive-optional-url-parameters.aspx
I know the question is for MVC2 but in MVC3, the answer given by Darin Dimitrov will fail.
Phil Haack explained the problem and gave the work around in his blog:
http://haacked.com/archive/2011/02/20/routing-regression-with-two-consecutive-optional-url-parameters.aspx