我正在使用Microsoft的实体框架6.1.3。首先使用数据库(来自Azure SQL Server实例(。自动生成的实体看起来像这样 - 数据库列是 dateTime2(0(,但实际上我只想坚持日期,但这是另一个讨论:
public partial class Liquidated
{
public long ID { get; set; }
public System.DateTime LiquidationDate { get; set; }
public string Comment { get; set; }
}
自动生成的控制器:
// POST: api/Liquidations
[ResponseType(typeof(Liquidated))]
public IHttpActionResult PostLiquidated(Liquidated liquidated)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
使用RestSharp的单元测试:
var client = new RestClient("http://localhost:64185/api/Liquidations");
var request = new RestRequest(Method.POST);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("authorization", "bearer " + token.access_token);
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddHeader("audience", "Any");
request.AddHeader("accept-language", "en-gb");
request.AddHeader("accept", "application/json");
var liquidation = new Liquidated();
liquidation.LiquidationDate = DateTime.Now;
liquidation.Comments = "Updated group name 1";
request.AddObject(liquidation);
IRestResponse<Liquidated> response = client.Execute<Liquidated>(request);
但是,当我在控制器中检查模型史塔特时,它是无效的,并显示了以下格式错误:
:"The value '18/04/2017 14:26:12' is not valid for LiquidationDate."
我已经搜索了许多帖子,试图强制格式化(这是C#Entities和SQL Server之间的日期格式化问题吗?(,但是我看不到我在做错了什么。任何帮助都非常感谢!
从 @davidg的评论中开始,我更改了客户端的清算类以包含字符串而不是datetime:
public partial class Liquidated
{
public long ID { get; set; }
public string LiquidationDate { get; set; }
public string Comments { get; set; }
}
然后在Restsharp之外进行格式:
var client = new RestClient("http://localhost:64185/api/Liquidations");
var request = new RestRequest(Method.POST);
request.AddHeader("postman-token", "a95ce9b5-8c7f-e223-faa6-3768f7edd10c");
request.AddHeader("cache-control", "no-cache");
request.AddHeader("authorization", "bearer " + token.access_token);
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddHeader("audience", "Any");
request.AddHeader("accept-language", "en-gb");
request.AddHeader("accept", "application/json");
var liquidation = new Liquidated();
liquidation.LiquidationDate = DateTime.Now.ToString("yyyy-MM-dd");
liquidation.Comments = "Updated group name 1";
// request.DateFormat = "yyyy-MM-dd"; // This is ignored by RestSharp
request.AddObject(liquidation);
IRestResponse<Liquidated> response = client.Execute<Liquidated>(request);
阅读了restsharp github问题日志:https://github.com/restsharp/restsharp/issues/629,没有计划支持此设施。
感谢您将我指向正确的方向。
如果将JSON数据发布到Web API,请确保以ISO 8601格式(2017-04-18T14:22:59Z
(发送日期值。否则,您遇到了模型的问题。
JS函数的示例将UTC JavaScript日期对象转换为ISO 8601字符串(仅日期没有时间(:
/// <summary>
/// Takes a utc js date and converts it to a iso8601 utc string
/// </summary>
GlobalJsHelpers.Iso8601FromUtc = function(utcDate) {
//create a two digit month string (01-12)
var month = ('0' + (utcDate.getUTCMonth() + 1)).substr(-2);
//create a two digit day string (01-31)
var day = ('0' + utcDate.getUTCDate()).substr(-2);
return utcDate.getUTCFullYear() + '-' + month + '-' + day;
};
请参阅为什么restsharp对两个日期进行不同的日期?关于如何欺骗restsharp。