7

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?

K. L.
  • 654
  • 2
  • 10
  • 21

1 Answers1

5

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.

Community
  • 1
  • 1
peco
  • 3,890
  • 1
  • 20
  • 27