Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send Timespan to WebAPI from Javascript

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?

like image 205
K. L. Avatar asked Oct 23 '14 15:10

K. L.


1 Answers

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.

like image 186
peco Avatar answered Oct 11 '22 16:10

peco