Reputation: 38033
I have two different API endpoints. Both controllers inherit ApiController
. Both return a single object, each of which has a field of type DateTime
.
API 1:
[Route("OtherThings/{otherThingId:guid}/FirstThing", Name = "GetFirstThing")]
[HttpGet]
[ResponseType(typeof(Response1))]
public IHttpActionResult GetFirstThing(Guid otherThingId, [FromUri] DateTime? modifiedDate = null)
{
var things = GetThings(otherThingId, modifiedDate);
if (!things.Any())
{
return NotFound();
}
return Ok(new Response1(things.First()));
}
where Response1 is defined as:
public class ReadingProgressResponse
{
public DateTime DateTime { get; set; }
// and other properties
}
sample response:
{
"dateTime": "2016-09-28T14:30:26"
}
API 2
[HttpGet]
[Route("{someId:guid}", Name = "SomeName")]
[ResponseType(typeof (Response2))]
public IHttpActionResult GetResponse2(Guid someId)
{
var data = GetSomeData(someId);
return Ok(new Response2(data));
}
where Response2
is defined as:
public class ScreenSessionResponse
{
public DateTime ExpirationTime { get; set; }
// and other fields
}
Sample response:
{
"expirationTime": "2016-09-28T14:48:09Z"
}
Notice that API 1 does not have a "Z" at the end of the date, but API 2 does.
Why? Do I have any control over how the response gets formatted?
Upvotes: 1
Views: 99
Reputation: 3154
Yes, you have control on how dates are formatted.
Try this code in the OnStart method of your global.asax:
// Convert all dates to UTC
var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc;
For more info, have a look here
Upvotes: 1