我正在尝试反序列化web api控制器中的对象。它由DateTime组成。日期时间属性的格式为" dd-MM-YYYY"。我在global.asax文件中设置了json serialzer 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.");
}
}