问题
I have two moment dates and am calculating the duration by doing the diff between both.
How can I post the duration to the WebAPI controller so that it is automatically deserialize to a C# TimeSpan? When I post "P2DT00H" it will deserialize properly to a TimeSpan but how can I get that format from a moment duration?
回答1:
Calculate the duration as described in this answer Get the time difference between two datetimes
Define an endpoint like this:
public IHttpActionResult Post([FromBody] TimeSpan ts)
{
// do stuff
return Ok(ts);
}
And post the duration like so:
POST http://localhost/theendpoint HTTP/1.1
Content-Length: 12
Content-Type: application/json
Host: localhost
"00:01:10"
And web api will deserialize it and bind it to the model correctly.
来源:https://stackoverflow.com/questions/26531179/how-to-send-timespan-to-webapi-from-javascript