ASP.NET Web API错误响应和(application / json vs text / json)

时间:2015-10-27 09:19:28

标签: json asp.net-mvc http asp.net-web-api

我想在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"
}

两个问题:

  • 返回错误代码(不是http代码)的最佳做法
  • 如何配置ASP.NET应用程序以返回我的自定义响应。

0 个答案:

没有答案
相关问题