MVC3異常處理的方法


 

 

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方法


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM