简体   繁体   English

HTTP 错误的自定义 json 响应正文

[英]Custom json response body for HTTP errors

I'm wondering if there is a way in API Controllers to return a custom object as response body for methods like: BadRequest() or NotFound() .我想知道 API 控制器中是否有一种方法可以将自定义对象作为响应体返回给诸如BadRequest()NotFound()

For example, for 404 error, I'd like to return something like:例如,对于404错误,我想返回如下内容:

{
  "StatusCode": 404,
  "Error": "Not Found",
  "Message": "Custom message..."
}

Instead I'm getting this:相反,我得到了这个:

{
  "Message": "Custom message..."
}

At the moment to return complex response body I'm using Ok() this way:目前要返回复杂的响应主体,我正在使用Ok()这种方式:

return Ok(new
{
    Success = false,
    Message = "Custom message...",
    // other fields...
});

But obviously I'm returning a 200 status that is not so meaningful.但很明显,我要返回一个意义不大的200状态。

Is there a better way to achieve this?有没有更好的方法来实现这一目标?

Long Way很长的路要走

If you need a quick solution just jump to Short Way , read this just to understand how it works under the hood.如果您需要快速解决方案,只需跳转到Short Way ,阅读本文即可了解它是如何工作的。 Derive your own JsonErrorResult class derived from JsonResult :JsonResult派生您自己的JsonErrorResult类:

public sealed JsonErrorResult : JsonResult
{
    public JsonErrorResult(StatusCodes statusCode, object value)
        : base(value)
    {
        _statusCode = statusCode;
    }

    private readonly JsonErrorResult StatusCodes _statusCode;
}

Now override ExecuteResultAsync() method to change status code of default JsonResult implementation:现在覆盖ExecuteResultAsync()方法以更改默认JsonResult实现的状态代码:

public override Task ExecuteResultAsync(ActionContext context)
{
    context.HttpContext.Response.StatusCode = _statusCode;
    return base.ExecuteResultAsync(context);
}

You simply return calling BadRequest() you simply do this:您只需返回调用BadRequest()您只需执行以下操作:

return new JsonErrorResult(StatusCodes.Status400BadRequest, new 
{
    StatusCode = "404",
    Error = "bla bla bla",
    Message = "bla bla bla"
});

Of course if you use it often you may want to create your own helper method:当然,如果你经常使用它,你可能想创建自己的辅助方法:

protected static JSonErrorResult JsonError(StatusCodes statusCode,
                                           string error, string message)
{
    return new JsonErrorResult(statusCode, new 
    {
        StatusCode = Convert.ToString((int)statusCode),
        Error = error,
        Message = message
    });
}

Used like this:像这样使用:

return JsonError(StatusCodes.Status400BadRequest, "bla bla bla", "bla bla bla");

Short Way捷径

JsonResult already has StatusCode property then your helper method may become like this: JsonResult已经有StatusCode属性,那么你的辅助方法可能会变成这样:

protected static JSonResult JsonError(StatusCodes statusCode,
                                           string error, string message)
{
    var result = new JsonResult(new 
    {
        StatusCode = Convert.ToString((int)statusCode),
        Error = error,
        Message = message
    });

    result.StatusCode = (int)statusCode;

    return result;
}

I usually return something like this:我通常会返回这样的东西:

return Json(new { success = false, data = "" }, JsonRequestBehavior.AllowGet);

And then in my AJAX success, I do:然后在我的 AJAX 成功中,我这样做:

if(result.success){
    result.data...
}else{
    //error...
}

If you do not want to always send a 200 code, then can you do something like this?如果您不想总是发送 200 代码,那么您可以这样做吗?

//if success...
Response.StatusCode = 200;
return Json(new { responseCode = 200, data = "", message = "..." }, JsonRequestBehavior.AllowGet);

//if failure...
Response.StatusCode = 404;
return Json(new { responseCode = 404, data = "", message = "..." }, JsonRequestBehavior.AllowGet);

Create function to deal with error.创建函数来处理错误。

function AjaxCompletion(xhr) {
    switch (xhr.status) {
        case 200:
            //Your Message
            break;
        case 401: //unauthorize
           //Your desire message
            break;
    }
}

And in AJAX complete :AJAX complete

  complete: function (xhr) {
            AjaxCompletion(xhr);
        }

Thanks for your code, it worked (after a while).感谢您的代码,它起作用了(一段时间后)。 It seems that you have written pseudo-code rather than code itself.看来您编写的是伪代码而不是代码本身。 So, in order to make life easier to other, here is your same exact code but withing a class which runs.因此,为了让其他人的生活更轻松,这里是您的完全相同的代码,但有一个运行的类。

public sealed class JsonErrorResult : JsonResult {

    private readonly HttpStatusCode _statusCode;

    public JsonErrorResult(HttpStatusCode sCode, object value) : base(value) {
        _statusCode = sCode;
    }

    public override Task ExecuteResultAsync(ActionContext context) {
        context.HttpContext.Response.StatusCode = (int)_statusCode;
        return base.ExecuteResultAsync(context);
    }

    public static JsonResult JsonError(HttpStatusCode statusCode,
                                               string message, string error = "") {
        var result = new JsonResult(new {
            StatusCode = Convert.ToString((int)statusCode),
            Error = error,
            Message = message
        });

        result.StatusCode = (int)statusCode;

        return result;
    }
}

and finally you call it like so:最后你这样称呼它:

return JsonErrorResult.JsonError(System.Net.HttpStatusCode.Unauthorized,
                                "error msg");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM