我想在ASP.NET Web Api中使用响应返回错误代码,因此在控制器中我会执行以下操作:
case ErrorCodes.TimeOut:
{
HttpError e = new HttpError(fex.Message) { { "ErrorCode", fex.ErrorCode }, { "Tag", fex.Tag } };
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, error: e);
}
当我使用text / json
调用此api时,这没关系Content-Type: text/json; charset=utf-8
{"Message":"Timed out at 8:05 AM","ErrorCode":102,"Tag":"1445933100"}
但如果我使用空内容类型或application / json(默认设置),我会得到一些像这样的标准响应
Content-Type: application/json; charset=utf-8
{
"status": 400,
"source": "my source",
"message": "Timed out at 8:05 AM"
}
两个问题: