提问人:Safi Mustafa 提问时间:12/8/2015 最后编辑:Safi Mustafa 更新时间:12/8/2015 访问量:1193
在牛顿软件.js 中包含日期时间的 DeserializeObject 时出错
Error when DeserializeObject which consist DateTime in newtonsoft.js
问:
我正在尝试反序列化 Web API 控制器中的对象。它由 DateTime 组成。date Time 属性的格式为“dd-MM-YYYY”。我在global.asax文件中设置了json序列器dd-MM-YYYY的格式。它仍然给我错误“无法将字符串转换为日期时间”。
将两个文件附加为图像
------------------纯文本代码-------------------
Global.asax 文件应用程序启动函数
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
//BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
GlobalConfiguration.Configuration.MessageHandlers.Add(new ApiAuthenticationHandler());
JobScheduler.Start();
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
DateFormatString = "dd-MM-yyyy hh:mm tt",
};
var dateTimeConverter = new IsoDateTimeConverter
{
DateTimeFormat = "dd-MM-yyyy hh:mm tt"
};
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings
.Converters.Add(dateTimeConverter);
}
API 请求
{
"CustomerViewModel": {
"DOB": "18-12-2018",
}
}
API 控制器
public Guid CustomerSignup(JObject parametrs)
{
dynamic jsonParameters = parametrs;
CustomerViewModel customer = jsonParameters.CustomerViewModel.ToObject<CustomerViewModel>();
if (ModelState.IsValid)
{
try
{
Guid CurrentApplicationId = new Guid(ConfigurationManager.AppSettings["ApplicationId"].ToString());
Logic logic = new Logic(CurrentApplicationId);
Guid result = logic.AddCustomers(customer, false);
if (result != new Guid())
{
logic.SendVerifcationEmail(result, customer.UserName, customer.UserAddress.Email, Request.RequestUri.GetLeftPart(UriPartial.Authority));
return result;
}
else
{
return result;
}
}
catch (Exception ex)
{
if (ex.GetType().Name == "FaultException")
{
throw new FaultException(ex.Message);
}
else
{
throw new FaultException("Fields are not valid.");
}
}
}
else
{
throw new FaultException("Fields are not valid.");
}
}
答: 暂无答案
评论