码迷,mamicode.com
首页 > Web开发 > 详细

MVC 异常过滤

时间:2018-04-18 19:09:24      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:iis   contex   .com   put   new   ati   project   led   覆盖   

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

 

MVC 异常过滤

标签:iis   contex   .com   put   new   ati   project   led   覆盖   

原文地址:https://www.cnblogs.com/chenyishi/p/8876026.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!