1

What will be the URL for this Get method?

public class ValuesController : ApiController
{
    public string Get(DateTimeOffset startDate)
    {
        return "value";
    }
}

The route template is

routeTemplate: "api/{controller}/{startDate}"

I tried to hit with URLs like

api/values/12Sept2013
api/values/2011-06-01T14:03:00+00:00Z
api/values/2011-12-13

nothing worked.. I even encoded the startDate with HttpUtility.UrlEncode, but won't work!

Any idea what is the correct format?

dav_i
  • 27,509
  • 17
  • 104
  • 136
Deepak Raj
  • 730
  • 2
  • 9
  • 26

2 Answers2

2

something like this

~/api/values?startDate=2008%2F08%2F08
Milad Hosseinpanahi
  • 1,495
  • 12
  • 20
0

Answer

To send a DateTimeOffset to your API, format it like this:

2017-04-17T05:45:18.070Z

The complete API URL will look like this:

http://localhost:1234/api/values/startDate=2017-04-17T05:45:18.070Z

By formatting it this in this manner, I am able to pass in a DateTimeOffset parameter to my ApiController that I made using Azure Mobile Services.

Code

You can use ToString(yyy-MM-ddTHH:mm:ss.fffZ) to parse the DateTimeOffset.

var dateTimeOffsetAsAPIParameter = DateTimeOffset.Now.UtcDateTime.ToString("yyy-MM-ddTHH:mm:ss.fffZ");

string.Format("http://localhost:1234/api/values/?startDate={0}", dateTimeOffsetAsAPIParameter);
Community
  • 1
  • 1
Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123