c# – 如何从自定义异常过滤器返回JSON结果?

前端之家收集整理的这篇文章主要介绍了c# – 如何从自定义异常过滤器返回JSON结果?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想创建一个自定义异常过滤器,它将捕获返回 JSON结果的控制器操作中抛出的异常.

我想重构以下操作方法

public JsonResult ShowContent()
    {
        try
        {
            // Do some business logic work that might throw a business logic exception ...
            //throw new ApplicationException("this is a business exception");

            var viewmodel = new DialogModel
                                {
                                    FirstName = "John",LastName = "Doe"
                                };

            // Other exceptions that might happen:
            //throw new sqlException(...);
            //throw new OtherException(...);
            //throw new ArgumentException("this is an unhandeled exception");

            return
                Json(
                    new
                        {
                            Status = DialogResultStatusEnum.Success.ToString(),Page = this.RenderPartialViewToString("ShowContent",viewmodel)
                        });
        }
        catch (ApplicationException exception)
        {
            return Json(new { Status = DialogResultStatusEnum.Error.ToString(),Page = exception.Message });
        }
        catch (Exception exception)
        {
            return Json(new { Status = DialogResultStatusEnum.Exception.ToString(),Page = "<h2>PROBLEM!</h2>" });
        }
    }
}

我想要做的是创建一个自定义异常过滤器属性,该属性将捕获操作中引发的任何异常,遵循以下逻辑:

>检查是否有异常

>不:回来
>是的:

>如果是BusinessLogic异常 – 返回JSON结果
>如果是其他未处理的异常:

>记录
>使用不同的结果代码返回另一个JSON结果

解决方法

public class YourController : BaseController
    {
        public JsonResult showcontent()
        {
            // your logic here to return foo json
            return Json ( new { "dfd" }); // just a dummy return text replace it wil your code
        }
    }

    public class BaseController : Controller
    {
 // Catch block for all exceptions in your controller
        protected override void OnException(ExceptionContext filterContext)
        {
            base.OnException(filterContext);
            if (filterContext.Exception.Equals(typeof(ApplicationException)))
            {
                //do  json and return
            }
            else
            {
                // non applictaion exception
// redirect to generic error controllor and error action which will return json result

            }
        }

    }

请参阅此链接以了解如何创建和使用HandleError attribute

*编辑HandleAttribute for Actions *

//not tested code
public class HandleErrorfilter : HandleErrorAttribute
    {
         public string ErrorMessage { get; set; }

        public override void OnException(ExceptionContext filterContext)
        {
                string message = string.Empty;
                //if application exception
                // do  something
                 else
                    message = "An error occured while attemting to perform the last action.  Sorry for the inconvenience.";
            }
            else
            {
                base.OnException(filterContext);
            }
        }

      public class YourController : BaseController
        {
            [HandleErrorfilter]
            public JsonResult showcontent()
            {
                // your logic here to return foo json
                return Json ( new { "dfd" }); // just a dummy return text replace it wil your code
            }
        }
原文链接:https://www.f2er.com/csharp/97994.html

猜你在找的C#相关文章