我正在迁移一个旧的应用程序,我需要一个特定的web api 2方法返回这样的日期:/Date(46546)/
而不是ISO 8601格式:2016-10-31T07:22:57.1153868-05:00
我的web api方法如下:
[Route("GetListData/{jtStartIndex:int=0}/{jtPageSize:int=0}/{jtSorting?}")]
[HttpPost]
public HttpResponseMessage GetListData(int jtStartIndex, int jtPageSize, string jtSorting)
{
try
{
var dataList = DataContainer.Instance.Data;
//HERE dataList is a collection of a custom model, a model that have properties of datetime type.
return this.Request.CreateResponse(HttpStatusCode.OK,
new { Result = "OK", Records = dataList, TotalRecordCount = dataList.Count });
}
catch (Exception ex)
{
return this.Request.CreateResponse(
HttpStatusCode.InternalServerError,
new { Result = "ERROR", Message = ex.Message });
}
}
据我所知,较新的WebAPI使用JSON。. NET作为默认的序列化器,它遵循JSON标准,因此不能序列化您请求的格式,这是不符合JSON标准的。
旧的WebAPI使用微软自己的DataContractJsonSerializer,它只支持非标准兼容格式。
返回到序列化器:
首先,你必须在可用的序列化器列表的末尾添加一个序列化器到你的WebAPI项目,它使用旧的序列化器库。
然后你可以为某个控制器选择那个序列化器