返回带有错误状态代码 MVC 的 JSON [英] Return JSON with error status code MVC

查看:27
本文介绍了返回带有错误状态代码 MVC 的 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图按照中的建议将错误返回到对控制器的调用此链接,以便客户端可以采取适当的措施.控制器由 javascript 通过 jquery AJAX 调用.只有当我没有将状态设置为错误时,我才会返回 Json 对象.这是示例代码

I was trying to return an error to the call to the controller as advised in This link so that client can take appropriate action. The controller is called by javascript via jquery AJAX. I am getting the Json object back only if I don't set the status to error. Here is the sample code

if (response.errors.Length > 0)
   Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Json(response);

如果我不设置状态码,我会得到 Json.如果我设置状态代码,我会返回状态代码,但不会返回 Json 错误对象.

I get the Json if I don't set the statuscode. If I set the status code I get the status code back but not the Json error object.

更新我想将一个 Error 对象作为 JSON 发送,以便它可以处理 ajax 的错误回调.

Update I want to send an Error object as JSON so that it can be handled error callback of ajax.

推荐答案

我找到了解决方案 这里

我必须创建一个动作过滤器来覆盖 MVC 的默认行为

I had to create a action filter to override the default behaviour of MVC

这是我的异常类

class ValidationException : ApplicationException
{
    public JsonResult exceptionDetails;
    public ValidationException(JsonResult exceptionDetails)
    {
        this.exceptionDetails = exceptionDetails;
    }
    public ValidationException(string message) : base(message) { }
    public ValidationException(string message, Exception inner) : base(message, inner) { }
    protected ValidationException(
    System.Runtime.Serialization.SerializationInfo info,
    System.Runtime.Serialization.StreamingContext context)
        : base(info, context) { }
}

请注意,我有初始化我的 JSON 的构造函数.这是动作过滤器

Note that I have constructor which initializes my JSON. Here is the action filter

public class HandleUIExceptionAttribute : FilterAttribute, IExceptionFilter
{
    public virtual void OnException(ExceptionContext filterContext)
    {
        if (filterContext == null)
        {
            throw new ArgumentNullException("filterContext");
        }
        if (filterContext.Exception != null)
        {
            filterContext.ExceptionHandled = true;
            filterContext.HttpContext.Response.Clear();
            filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
            filterContext.HttpContext.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
            filterContext.Result = ((ValidationException)filterContext.Exception).myJsonError;
        }
    }

现在我有了动作过滤器,我将用过滤器属性装饰我的控制器

Now that I have the action filter, I will decorate my controller with the filter attribute

[HandleUIException]
public JsonResult UpdateName(string objectToUpdate)
{
   var response = myClient.ValidateObject(objectToUpdate);
   if (response.errors.Length > 0)
     throw new ValidationException(Json(response));
}

当错误被抛出时,实现 IExceptionFilter 的动作过滤器被调用,我在错误回调的客户端上取回 Json.

When the error is thrown the action filter which implements IExceptionFilter get called and I get back the Json on the client on error callback.

这篇关于返回带有错误状态代码 MVC 的 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆