zoukankan      html  css  js  c++  java
  • Asp.net MVC 自定义异常处理类

    using ElegantWM.Common;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using ElegantWM.Factory;
    using System.Data.SqlClient;
    
    namespace ElegantWM.WebUI
    {
        public class ExceptionHandler : HandleErrorAttribute
        {
            public static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            public override void OnException(ExceptionContext filterContext)
            {
    
                Exception Error = filterContext.Exception;
                while (Error.InnerException != null)
                {
                    Error = Error.InnerException;
                }
                //获取出现异常的controller名和action名,用于记录
                string controllerName = (string)filterContext.RouteData.Values["controller"];
                string actionName = (string)filterContext.RouteData.Values["action"];
                string Message = Error.GetType().Name + ":" + Error.Message;//错误信息 
                string Url = HttpContext.Current.Request.RawUrl;//错误发生地址
                log.Error("URL=" + Url + ";Msg=" + Message, Error);
                filterContext.ExceptionHandled = true;
                if (filterContext.HttpContext.Request.IsAjaxRequest())
                {
                    filterContext.Result = new JsonResult
                    {
                        Data = ResultMsg.Failure(Message),
                        JsonRequestBehavior = JsonRequestBehavior.AllowGet
                    };
                }
                else
                {
                    //定义一个HandErrorInfo,用于Error页使用
                    HandleErrorInfo model = new HandleErrorInfo(Error, controllerName, actionName);
                    //ViewResult是ActionResult,经常出现在controller中action方法return后面,但是出现形式是View()
                    ViewResult result = new ViewResult
                    {
                        ViewName = this.View,
                        MasterName = this.Master,
                        //定义ViewData,使用的是泛型
                        ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
                        TempData = filterContext.Controller.TempData
                    };
                    filterContext.Result = result;
                    filterContext.HttpContext.Response.Clear();
                    filterContext.HttpContext.Response.StatusCode = 500;
                    filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
                }
                base.OnException(filterContext);
            }
        }
    }
  • 相关阅读:
    在operator =中要处理“自我赋值”
    delete指针以后应赋值为NULL
    【转】C++对成员访问运算符->的重载
    【转】浅析SkipList跳跃表原理及代码实现
    【KakaJSON手册】05_JSON转Model_05_动态模型
    【KakaJSON手册】04_JSON转Model_04_值过滤
    【KakaJSON手册】03_JSON转Model_03_key处理
    【KakaJSON手册】02_JSON转Model_02_数据类型
    【KakaJSON手册】01_JSON转Model_01_基本用法
    利用eclipse调试JDK源码
  • 原文地址:https://www.cnblogs.com/qidian10/p/3420233.html
Copyright © 2011-2022 走看看