So I have a WebAPI 2 controller written in C# that takes among other things a query parameter of type DateTime. This is an API that returns all values from the data store based on a date filter. Something like, let's say:
public MyThing GetThing([FromUri]DateTime startTime)
{
// filter and return some results
}
I am running into 2 problems:
- For some reason despite passing in a ISO 8601 UTC formatted (with a Z) date, WebAPI is de-serializing it as a local DateTime, instead of Utc. This is obviously undesirable. I am not sure how to modify the pipeline to have it correctly understand UTC-0 DateTimes.
- I am returning a link back to the resource as part of the response body, in which I use the UrlHelper objects (obtained from the parent ApiController abstract class) Link() method to generate an href. I am passing a collection of query parameters I want added to the route. For whatever reason passing the DateTime formats it in a non-ISO8601 format. I can't find where this is controlled. I don't want to explicitly ToString() it as that's not enforceable universally.
In short, I want to figure out how to make sure that
- DateTimes that are passed in via FromUri query params are properly understood as ISO8601, including appropriate time zone offsets
- UrlHelper.Link() generates ISO8601-compliant DateTimes in the output URI string in a universally enforceable statically-typed way.
WebAPI 2 does provide wonderful hooks for formatting JSON, which I do make use of, so simply returning a DateTime in a JSON body formats it as desired using the ISO8601 format, and as well it is correctly understood in a [FromBody] JSON body. I can't find ways for pulling strings around URI handling though, and I would really like to!