我有两个不同的API端点。两个控制器都继承ApiController
。两者都返回一个对象,每个对象都有一个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()));
}
其中Response1定义为:
public class ReadingProgressResponse
{
public DateTime DateTime { get; set; }
// and other properties
}
样本回复:
{
"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));
}
其中Response2
定义为:
public class ScreenSessionResponse
{
public DateTime ExpirationTime { get; set; }
// and other fields
}
示例回复:
{
"expirationTime": "2016-09-28T14:48:09Z"
}
请注意,API 1在日期末尾没有“Z”,但是API 2确实如此。
为什么呢?我是否可以控制响应的格式化?