zoukankan      html  css  js  c++  java
  • Asp.Net MVC3(三)-MvcApp实现全局异常捕获

    定义异常捕获类:

    [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
        public class ExceptionAttribute : FilterAttribute, IExceptionFilter
        {
            public virtual void OnException(ExceptionContext filterContext)
            {
                string message = string.Format("消息类型:{0}<br>消息内容:{1}<br>引发异常的方法:{2}<br>引发异常源:{3}"
                    , filterContext.Exception.GetType().Name
                    , filterContext.Exception.Message
                     , filterContext.Exception.TargetSite
                     , filterContext.Exception.Source + filterContext.Exception.StackTrace
                     );
                
                //记录日志
                WriteLog.WriteInfo(message);
    
                //抛出异常信息
                filterContext.Controller.TempData["ExceptionAttributeMessages"] = message;
    
                //转向
                filterContext.ExceptionHandled = true;
                filterContext.Result = new RedirectResult(Globals.ApplicationDirectory + "/Error/ErrorDetail/");
            }
        }
    

      

    Mvc全局使用异常捕获类:

    Global.asax.cs文件注册全局异常捕获类

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
      filters.Add(new Models.ExceptionAttribute());


      filters.Add(new HandleErrorAttribute());
    }

    实现其他全局过滤的思想同上.

  • 相关阅读:
    Java中字符串中子串的查找共有四种方法(indexof())
    idea常用快捷键
    用hive或mr清洗app数据
    使用kafka作为生产者生产数据到hdfs(单节点)
    使用kafka作为生产者生产数据到hdfs
    c++ map的使用
    c++ set集合的使用
    c++ 木块问题
    c++ 大理石
    c 蛇形数字
  • 原文地址:https://www.cnblogs.com/snlfq2000/p/3236469.html
Copyright © 2011-2022 走看看