zoukankan      html  css  js  c++  java
  • ASP.NET MVC 异常Exception拦截器Fillter

    异常信息的处理在程序中非常重要, 在asp.net mvc中提供异常属性拦截器进行对异常信息的处理,异常拦截器也没有什么的,只是写一个类,继承另一个类(System.Web.Mvc.FilterAttribute)和一个接口(System.Web.Mvc.IExceptionFilter),实现接口里面OnException方法。

    代码实例:

    异常拦截器类:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Web;
     5 
     6 namespace AttributeDemo.Common
     7 {
     8     /// <summary>
     9     /// 异常信息拦截器
    10     /// </summary>
    11     public class ExceptionFillterAttribute : System.Web.Mvc.FilterAttribute, System.Web.Mvc.IExceptionFilter
    12     {
    13         #region 请求的action发生异常时会执行此方法
    14         /// <summary>
    15         /// 请求的action发生异常时会执行此方法
    16         /// </summary>
    17         /// <param name="filterContext"></param>
    18         void System.Web.Mvc.IExceptionFilter.OnException(System.Web.Mvc.ExceptionContext filterContext)
    19         {
    20             //在这里你可以记录发生异常时你要干什么,比例写日志
    21             string message = filterContext.Exception.Message;
    22             filterContext.Controller.ViewData["ErrorMessage"] = message;
    23 
    24             //返回的结果给客户端
    25             filterContext.Result = new System.Web.Mvc.ContentResult()
    26             {
    27                 Content = "出错了:)",
    28                 ContentEncoding = System.Text.Encoding.UTF8
    29             };
    30 
    31 
    32             filterContext.ExceptionHandled = true;  //告诉系统,这个异常已经处理了,不用再处理
    33 
    34             //filterContext.ExceptionHandled = false;  //告诉系统,这个异常没有处理,需要再处理 
    35         }
    36         #endregion
    37 
    38 
    39     }
    40 }

    控制器类:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Web;
     5 using System.Web.Mvc;
     6 
     7 namespace AttributeDemo.Controllers
     8 {
     9     /// <summary>
    10     /// 测试异常拦截器
    11     /// </summary>
    12     [AttributeDemo.Common.ExceptionFillter]   //这个异常拦截属性写在这里表示对该控制器所有的action的异常都进行拦截
    13     public class ExceptionFillterTestController : Controller
    14     {
    15         //
    16         // GET: /ExceptionFillter/
    17 
    18         /// <summary>
    19         /// 测试异常拦截
    20         /// </summary>
    21         /// <returns></returns>
    22         //[AttributeDemo.Common.ExceptionFillter]  //这个异常拦截属性写在这里表示只对该action的异常信息进行拦截
    23         public ActionResult TestExceptionFillter()
    24         {
    25             int i = int.Parse("sd");  //这里故意引发异常进行测试
    26             return View();
    27         }
    28 
    29         public ActionResult Index()
    30         {
    31             return View();
    32         }
    33 
    34     }
    35 }

    当请求action名称为TestExceptionFillter是时,action方法引发了异常,就会执行异常拦截类里面的OnException方法进行处理,处理结果请看下图:

  • 相关阅读:
    HDU 1394 Minimum Inversion Number
    HDU 4931 Happy Three Friends
    BZOJ 1089 严格n元树 (递推+高精度)
    BZOJ 1088 扫雷Mine (递推)
    BZOJ 3038 上帝造题的七分钟2 (并查集+树状数组)
    BZOJ 3211 花神游历各国 (树状数组+并查集)
    BZOJ 1087 互不侵犯King (位运算)
    BZOJ 1002 轮状病毒 (基尔霍夫矩阵)
    BZOJ 1005 明明的烦恼 (组合数学)
    BZOJ 1058 报表统计 (STL)
  • 原文地址:https://www.cnblogs.com/linJie1930906722/p/5770180.html
Copyright © 2011-2022 走看看