我有一个网络方法,它接收一个包含日期属性的对象。 我正在将表示对象的 json 传递给 web 方法。 asp.net 自动将 json 字符串转换为对象,因此我的方法如下所示:
[WebMethod]
public static Object Save(MyObject item)
{
....
}
public class MyObject
{
DateTime date;
.....
}
当日期以美国格式发送时,一切正常:'MM/dd/yyyy'
当日期以欧洲格式发送时:'dd/MM/yyy'
我收到错误消息。
我在全局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);
}
这段代码运行正确,它从 session 中读取文化 ID 并设置它。 从数据库登录时,文化 ID 会被读入 session 。
但这似乎对我收到错误的 web 方法预期的日期格式没有影响:
"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"}
我如何为网络方法设置预期的日期格式(我有很多这样的网络方法,所以显然我想要一个适用于所有这些方法的解决方案)。
谢谢
最佳答案
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
鉴于您正在接受 JSON,这应该不是对文化敏感的 IMO。在客户端将所有数据(包括数字和日期/时间)转换为不区分文化的格式。
因此,如果您在 UI 中接受日期输入,请让 JavaScript 在本地将其转换为日期/时间对象,然后将其转换为您想要使用的任何特定格式。 (我个人建议使用 yyyy-MM-ddTHH:mm:ss.fffZ
作为一种非常明确的格式,但我们开始吧。)
将 JSON 视为一种机器可读的序列化格式,它碰巧也很容易被人类阅读。在客户端保留所有特定于文化的解析将使您的生活更加简单。
编辑:从根本上说,您需要弄清楚您的值在 JSON 中的含义。如果它是供人类使用的文本,则将其视为字符串,不要尝试解析它,并以您想要显示的任何文化生成它。如果它是机器消费的日期/时间值,则以这种方式对待它并以文化中立的方式(固定格式,不变文化)生产它。尝试将值视为人类可读和机器可读的混合将给您带来这样的问题。
关于c# - 如何配置 asp.net webforms WebMethod 日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14358676/