ASP.NET MVC 模型绑定器通过 GET 和 POST 请求以不同的方式解析十进制
服务器托管 Asp.net mvc3 应用程序,并且浏览器区域性设置为 da(丹麦语),
GET request url: /get?d=1.1 (note that the decimal separator is .)
return: da;1,1 (note that the decimal separator is ,)
GET request url: /get?d=1,1 (the decimal separator is ,)
return: Exception Details: System.ArgumentException: The parameters dictionary contains a null entry for parameter 'd' of non-nullable type 'System.Decimal' for method 'System.Web.Mvc.ContentResult get(System.Decimal)' in 'Intranet.Controllers.OrderController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters
但是给定 post 请求相同的输入,结果完全相反。
POST request url: /get2 (form data d=1.1)
return: Exception ...
POST request url: /get2 (form data d=1,1)
return: da;1,1
我认为 POST 请求正在按预期工作。但为什么 GET 请求的行为不同呢?在这种情况下,默认模型绑定器如何工作。
The server is hosting Asp.net mvc3 app and the Browser culture is set to da (Danish)
GET request url: /get?d=1.1 (note that the decimal separator is .)
return: da;1,1 (note that the decimal separator is ,)
GET request url: /get?d=1,1 (the decimal separator is ,)
return: Exception Details: System.ArgumentException: The parameters dictionary contains a null entry for parameter 'd' of non-nullable type 'System.Decimal' for method 'System.Web.Mvc.ContentResult get(System.Decimal)' in 'Intranet.Controllers.OrderController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters
But given the same input to a post request, the results are exactly opposite.
POST request url: /get2 (form data d=1.1)
return: Exception ...
POST request url: /get2 (form data d=1,1)
return: da;1,1
I suppose the POST request is working as expected. But why does the GET request behave differently? How does the default model binder work in this case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您通过帖子发送数据时,区域设置就会生效。当您通过 GET 发送数据时,它始终使用不变的区域设置。
这样做似乎是因为您可以复制并粘贴 URL,并将其发送给另一个国家/地区的某人。如果在 URL (GET) 中包含参数时考虑浏览器的语言,则 URL 将会中断(如果考虑日期格式而不是小数点分隔符,这一点会更明显)。
除此之外,.Net 团队的一名成员在这里提到了这一点: http://forums.asp.net/t/1461209.aspx/1?Nullable+DateTime+Action+Parameters+Parsed+in+US+format+irrespective+of+locale+
When you send the data through a post, the locales take effect. When you send the data through a GET, it always uses the invariant locale.
It seems this is done because you could copy and paste an URL, and send it to someone in another country. If the language of the browser was considered when a parameter is included in the URL (GET) the URL would break (it's more obvious if you think about date formats than decimal separators).
Among other places, it's mentioned here by a member of the .Net team: http://forums.asp.net/t/1461209.aspx/1?Nullable+DateTime+Action+Parameters+Parsed+in+US+format+irrespective+of+locale+