返回JSON错误状态code MVC [英] Return JSON with error status code MVC

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

问题描述

我是想作为建议将错误返回到调用控制器
<一href=\"http://stackoverflow.com/questions/6010368/how-does-json-determine-a-success-from-an-error\">This链接让客户端可以采取适当的行动。
该控制器由JavaScript通过jQuery AJAX调用。我得到的JSON对象只返回,如果我不将状态设置为错误。
下面是示例code

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的,如果我不设置状态code。
如果我设置状态code我得到的状态code回来,但不是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.

推荐答案

我找到了解决办法<一href=\"http://blog.tonysneed.com/2011/10/21/global-error-handling-in-asp-net-mvc-3-with-ninject/\">here

我要创建一个动作过滤器来覆盖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.

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

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