I have a webmethod that receives an object that contains a date property. I am passing a json that represents the object to the webmethod. asp.net automatically converts the json string to an object so my method looks like:
[WebMethod]
public static Object Save(MyObject item)
{
....
}
public class MyObject
{
DateTime date;
.....
}
This all works well when the date is sent in USA format: 'MM/dd/yyyy'
When the date is sent in European format: 'dd/MM/yyy' I receive an error.
I am setting the culture of the thread in global asax:
void Application_AcquireRequestState(object sender, EventArgs e)
{
var s = GetSessionValues();
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(s.CultureID);
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(s.CultureID);
}
This code runs correctly it reads the culture id from the session and sets it. The culture id is read into the session upon login from the database.
But this seems to have no affect on the date format expected by the web method i receive the error:
"31/08/2012 is not a valid value for DateTime.",
"StackTrace":"
at System.ComponentModel.DateTimeConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)\r\n
at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)\r\n
at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)\r\n
at System.Web.Script.Serialization.ObjectConverter.AssignToPropertyOrField(Object propertyValue, Object o, String memberName, JavaScriptSerializer serializer, Boolean throwOnError)\r\n
at System.Web.Script.Serialization.ObjectConverter.ConvertDictionaryToObject(IDictionary`2 dictionary, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)\r\n
at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)\r\n
at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)\r\n
at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToType(Object o, Type type, JavaScriptSerializer serializer)\r\n
at System.Web.Script.Services.WebServiceMethodData.StrongTypeParameters(IDictionary`2 rawParams)\r\n
at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters)\r\n
at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n
at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.FormatException"}
How can I set the expected date format for the webmethod (I have a lot of these web method so obviously I want a solution that would apply for all of them).
Thanks
I have a webmethod that receives an object that contains a date property. I am passing a json that represents the object to the webmethod. asp.net automatically converts the json string to an object
Given that you're accepting JSON, this should not be culture-sensitive IMO. Convert all data into (including numbers and dates/times) into a culture-insensitive format at the client side.
So if you're accepting date input in a UI, get the JavaScript to convert that into a date/time object locally, and then convert that into whatever specific format you want to use. (Personally I'd suggest using yyyy-MM-ddTHH:mm:ss.fffZ as a nicely unambiguous format, but there we go.)
Think of JSON as a machine-readable serialization format which happens to be easy to read for humans too. Keeping all the culture-specific parsing at the client-side will make your life much simpler.
EDIT: Fundamentally, you need to work out what your value in JSON means. If it's text for human consumption, then treat it as a string, don't try to parse it, and produce it in whatever culture you want to be displayed. If it's a date/time value for machine consumption, then treat it that way and product it in a culture-neutral way (fixed format, invariant culture). Trying to treat the value as a mixture of human-readable and machine-readable will give you problems like this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With