用GET和POST请求ASP.NET MVC模型绑定解析十进制不同 [英] ASP.NET MVC model binder parse decimal differently with GET and POST requests

查看:201
本文介绍了用GET和POST请求ASP.NET MVC模型绑定解析十进制不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

服务器承载Asp.net MVC3应用程序和浏览器区域性设置为DA(丹麦)

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

不过,考虑到输入到一个POST请求一样,结果是完全相反的。

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

我想POST请求按预期工作。但为什么GET请求不同的表现?如何在这种情况下,默认模型联编工作。

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.

推荐答案

当您通过邮局发送数据时,区域设置生效。当您通过GET发送的数据,它总是使用不变的语言环境。

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.

似乎这样做,因为你可以复制并粘贴URL,并将其发送给某人在另一个国家。如果当包含在URL中的一个参数被认为是浏览器的语言(GET)的URL会打破(这是比较明显的,如果你想的日期​​格式不是十进制分隔符)。

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).

在其他地方,它是这里所说的由.NET团队的成员:<一href=\"http://forums.asp.net/t/1461209.aspx/1?Nullable+DateTime+Action+Parameters+Parsed+in+US+format+irrespective+of+locale+\" rel=\"nofollow\">http://forums.asp.net/t/1461209.aspx/1?Nullable+DateTime+Action+Parameters+Parsed+in+US+format+irrespective+of+locale+

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+

这篇关于用GET和POST请求ASP.NET MVC模型绑定解析十进制不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆