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上下文數據及異常相關信息
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方法