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());
        }
    }
}

 

posted on 2018-04-18 16:56  chester·chen  阅读(175)  评论(0编辑  收藏  举报