问题
My application runs under ro-RO culture settings, configured in web.config globalization section. If I make a POST request like
POST /myapp/index
date=03-12-2010&value=something
the model binding maps this to correct date value of "03 December 2010", since the default datetime format for ro-RO culture is dd-MM-yyyy. If I change the request method to GET passing the same data the date value in my action becomes "12 March 2010" (MM-dd-yyyy datetime format)
GET /myapp/index?date=03-12-2010&value=something
$.getJSON('/Home/Index', $('form').serialize(), function(d) {
// ...
});
$.post('/Home/Index', $('form').serialize(), function(d) {
// ...
}, 'json');
So in this case "getJson" & "post" must return the same result, but I get different results because of datetime difference.
How can I enable the same parsing format for GET requests also?
I know I can use a more generic format such as yyyy-MM-dd for dates, but I am just curious why is this happening?
回答1:
From my previous answer on this subject: Nullable DateTime Parameter is never bound when calling the action
"This is intentional. Anything that is part of the URI (note the 'Uniform' in URI) is interpreted as if it were coming from the invariant culture. This is so that a user in the U.S. who copies a link and sends it over IM to a friend in the U.K. can be confident that his friend will see the exact same page (as opposed to an HTTP 500 due to a DateTime conversion error, for example). In general, dates passed in RouteData or QueryString should be in the format yyyy-mm-dd so as to be unambiguous across cultures.
If you need to interpret a QueryString or RouteData parameter in a culture-aware manner, pull it in as a string, then convert it to the desired type manually, passing in the desired culture. (DateTime.Parse has overloads that allow you to specify a culture.) If you do this, I recommend also taking the desired culture as a QueryString or a RouteData parameter so that the 'Uniform' part of URI isn't lost, e.g. the URL will look something like ...?culture=fr-fr&date=01-10-1990."
来源:https://stackoverflow.com/questions/4349589/globalization-problem-with-datetime-and-asp-net-mvc-3-model-binding