ASP.NET Web API错误和响应(应用程序/json与文本/json)

ASP.NET Web API错误和响应(应用程序/json与文本/json),json,asp.net-mvc,http,asp.net-web-api,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

我想在我的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应用程序以返回我的自定义响应