【发布时间】:2016-09-29 09:05:39
【问题描述】:
我有两个不同的 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 有。
为什么?我可以控制响应的格式吗?
【问题讨论】:
标签: asp.net json asp.net-mvc