• MVC 异常过滤


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using cpf360.Common;
    using cpf360.DTO;
    
    namespace hanli_project.Filters
    {
        public class HanliExceptionAttribute : IExceptionFilter
        {
            void IExceptionFilter.OnException(ExceptionContext filterContext)
            {
                Exception exception = filterContext.Exception;
                if (filterContext.ExceptionHandled)
                {
                    return;
                }
    
                //返回数据
                filterContext.Result = new JsonResult() { JsonRequestBehavior = JsonRequestBehavior.AllowGet, Data = new OutputData() { data = null, code = -1, message = "服务器繁忙" } };
                
                //记录日志
                Log4netHelper.LogError(exception);
    
                // 设置自定义异常已经处理,避免其他过滤器异常覆盖 
                filterContext.ExceptionHandled = true;
    
                // 在派生类重写时,设置或者重写一个值该值指定是否禁用ISS7.0中自定义错误 
                filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
    
            }
        }
    }
    using System.Web;
    using System.Web.Mvc;
    using hanli_project.Filters;
    
    namespace hanli_project
    {
        public class FilterConfig
        {
            public static void RegisterGlobalFilters(GlobalFilterCollection filters)
            {
                //注册异常过滤器
                filters.Add(new HanliExceptionAttribute());
                filters.Add(new HandleErrorAttribute());
            }
        }
    }
  • 相关阅读:
    iOS 最新版 CocoaPods 的安装流程
    AFNetworking 3.0.4 的使用
    NSPredicate谓词
    PHP基本类型操作
    MJExtension使用指导(转)
    字典转模型KVC和runtime二者实现与区别
    iOS之KVC字典转模型的底层实现
    runtime 总结(原创)
    Objective-C Runtime能做什么?
    Runtime那些事儿(消息机制)
  • 原文地址:https://www.cnblogs.com/chenyishi/p/8876026.html
Copyright © 2020-2023  润新知