Can anyone recreate the following bug I'm experiencing in routes with 2 or more optional parameters, in .NET 4.0 / MVC3?

爱⌒轻易说出口 提交于 2019-11-29 10:58:41

So, Phil Haack just released a blog post detailing that this is a known issue and will be fixed in the next release of the .NET framework, or maybe if we are lucky as a bug fix to the particular dll. I'm not exactly sure how they will fix it though. Whether they will outlaw more than 1 optional parameter, or whether they will resolve it in the manner I've outlined in the comments to Darin's answer. At least they know about it and can take steps to specify the expected behavior with more than one optional parameter more in the future.

Only the last parameter of a route definition can be optional and this rule has been enforced in ASP.NET MVC 3. In your example you have both test2 and test3 optional which is not possible. Consider the following URL:

root/test1/test2

Given this URL the routing engine cannot say whether test2="test2" and test3="" or test2="" and test3="test2". So why generating an URL which can never be parsed back to it's constituent tokens?

Now maybe that's a bug or a feature or call it whatever but in all cases you should avoid having such routes. IMHO the bug here is that the framework doesn't throw an exception telling you that only the last parameter in a route definition can be optional, but of course that's just my opinion.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!