WebHost/App_Start/CustomExceptionAttribute.cs

(完整代码) 1KB

using Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebUI
{
    /// <summary>
    /// 全局异常捕获处理
    /// </summary>
    public class CustomExceptionAttribute : FilterAttribute, IExceptionFilter   //HandleErrorAttribute
    {
        public void OnException(ExceptionContext filterContext)
        {
            if (filterContext.ExceptionHandled == true)
            {
                HttpException httpExce = filterContext.Exception as HttpException;
                //MVC处理HttpException的时候,如果为500 则会自动
                //将其ExceptionHandled设置为true,则系统就无法捕获异常
                if (httpExce!=null && httpExce.GetHttpCode() != 500)
                {
                    return;
                }
            }
            HttpException httpException = filterContext.Exception as HttpException;
            if (httpException != null)
            {
                filterContext.Controller.ViewBag.UrlRefer = filterContext.HttpContext.Request.UrlReferrer;
                //if (httpException.GetHttpCode() == 404)
                //{
                //    filterContext.HttpContext.Response.Redirect("~/home/notfound");
                //}
                //else if (httpException.GetHttpCode() == 500)
                //{
                //    filterContext.HttpContext.Response.Redirect("~/home/internalError");
                //}
            }
            //写入日志 记录
            LogHelper.Error(this, "", filterContext.Exception);
            //设置异常已经处理
            filterContext.ExceptionHandled = true;
        }
    }
}

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。