Asp.net的Web API日期时间格式 [英] Asp.net web API datetime format

查看:587
本文介绍了Asp.net的Web API日期时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个发送JSON POST请求我的控制器与格式DD / MM / YYYY和呼叫时间值一个WinForm客户端返回一个状态code 400。

I have a Winform client that sends a json post request to my controller with a datetime value of the format dd/MM/yyyy and the call returns a status code 400.

我想补充:

<globalization uiCulture="fr" culture="fr-FR" />

我的web.config文件,但它仍然没有工作。

to my web.config file but it's still not working.

编辑:
我还要补充一点,我有过在客户端没有控制权,我说:

I should add also That I have no control over the client and that I added:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/mm/yyyy}")]

我没有结果的模型

to my model with no result

推荐答案

如果您通过JSON发布这个,那么你应该能够为您的日期格式的JSON.NET转换器。实施细节这里

If you are posting this via JSON then you should be able to create a JSON.NET converter for your date format. Implementation details are here.

事实上,在研究这个答案,我发现SO 的WebAPI Json.NET这个完整的例子定制日期处理

In fact in researching this answer I found this full example on SO WebApi Json.NET custom date handling

显然只是改变MyDateTimeConvertor转换要的东西,它使用目前的文化和你spefified格式。

Obviously just alter the conversion in MyDateTimeConvertor to be something that uses the current culture and the format you spefified.

DateTime.ParseExact(reader.Value.ToString(), "dd/mm/yyyy", CultureInfo.CurrentCulture);

writer.WriteValue(((DateTime)value).ToString("dd/mm/yyyy"));

这篇关于Asp.net的Web API日期时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆