How to send Timespan to WebAPI from Javascript

纵饮孤独 提交于 2020-01-30 05:28:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!