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

MVC3异常处理的方法

时间:2016-12-03 08:04:07      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:void   key   ring   信息   指定   arp   clear   ber   错误   

1、采用内置的HandleErrorAttribute对象,跳转到指定错误页

示例:http://www.cnblogs.com/TomXu/archive/2011/12/15/2285432.html

 

2、实现IExceptionFilter过滤器接口

    其实方法1中也是实现的IExceptionFilter接口。但此方法中可以对异常信息进行处理,如记录异常日志、跳转到指定页面等

    2.1 创建一个BaseController类,集成于Controller类,并实现IExceptionFilter的OnException方法。该方法的ExceptionContext参数包含http上下文数据及异常相关信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class BaseController : Controller
    {
        protected override void OnException(ExceptionContext filterContext)
        {
 
            //if (!filterContext.ExceptionHandled && filterContext.Exception is ArgumentOutOfRangeException)
            if (!filterContext.ExceptionHandled)
            {
                filterContext.Result = new RedirectResult("~/Error.htm");
                filterContext.ExceptionHandled = true;
            }
 
            base.OnException(filterContext);
        }
    }

  

    2.2 所有的Controller类继承于BaseController类,这样挡在Controller中发生异常时就会触发OnException方法

 

 
 

MVC3异常处理的方法

标签:void   key   ring   信息   指定   arp   clear   ber   错误   

原文地址:http://www.cnblogs.com/webenh/p/6127909.html

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