2012-08-31 75 views
2

我已經閱讀了有關ASP.NET MVC中異常處理的文章。我想通過簡單介紹來確定我是否正確。任何人都可以請評論。是否正確的方式在ASP.NET MVC中的異常處理

  1. 如有必要,捕獲控制器操作中的異常。

    [HttpPost] 
    public ActionResult Insert() 
    { 
        try 
        { 
    
        } 
        catch 
        { 
         //ModelState.Error -> display error msg to the user. 
        } 
    } 
    
  2. 覆蓋在basecontroller控制器的「onException的」方法和「日誌」中步驟1和其他MVC例外

  3. 拋出的異常已登錄application_onerror全球異常。

+2

看看像ELMAH –

+0

是的,我打算使用log4net,但沒有決定。 – Sunny

+1

爲什麼要重新發明輪子?我承認很高興知道它是如何工作的,但是在你明白自己寫的東西沒有真正的意義,除非它不做你想做的事情,log4net的目標是稍微不同的日誌記錄,然後elmah,我將elmah看作更多意外的異常日誌記錄和log4net記錄應用程序事件/已知錯誤 –

回答

2

我肯定會推薦ELMaH,而不是自己編寫這個代碼,並且還會通過Log4Net來爲您的MVC應用程序編寫代碼。我個人會避免任何異常處理,除非我有特定的功能響應。通過這種方式,我不會「吃」ELMaH等應用程序範圍的工具將爲我優雅處理的任何錯誤。

ELMaH還有很好的內置網絡報告功能,還有專門用於ELMaH的第三方工具,可以爲您提供統計數據,例如,最常見的錯誤。

你可以用自定義錯誤重新開始......

<customErrors defaultRedirect="~/site/error" mode="RemoteOnly"> 
    <error statusCode="404" redirect="~/site/notfound" /> 
</customErrors> 

...一個控制器,它知道你正在使用ELMAH ...

public virtual ActionResult Error() { 
    System.Collections.IList errorList = new System.Collections.ArrayList(); 
    ErrorLog.GetDefault(System.Web.HttpContext.Current).GetErrors(0, 1, errorList); 
    ErrorLogEntry entry = null; 
    if (errorList.Count > 0) { 
     entry = errorList[0] as Elmah.ErrorLogEntry; 
    } 
    return View(entry); 
} 

...後盾一種觀點認爲,可以幫助遊客獲得特定錯誤ID給你:

@model Elmah.ErrorLogEntry 

@if (Context.User.Identity.IsAuthenticated) { 
    <p>Since you are signed in, we've noted your contact information, 
    and may follow up regarding this to help improve our product.</p> 
} else { 
    <p>Since you aren't signed in, we won't contact you regarding this.</p> 
} 
<p>Error ID: @Model.Id</p> 

我也注意到這是本次考試的HttpPost PLE。如果你正在做AJAX,那麼你會想要以獨特的方式處理錯誤。選擇一個標準的響應,你可以發送到瀏覽器,所有的AJAX代碼處理優雅。也許通過在javascript警報中顯示ELMaH錯誤ID(作爲一個簡單示例)。

我也通過Global.asax中處理一些特殊類型的AJAX的錯誤:

protected void Application_EndRequest() 
{ 
    if (Context.Response.StatusCode == 302 && 
     Context.Request.Headers["X-Requested-With"] == "XMLHttpRequest") 

HandleErrorAttribute是一個不錯的功能,但它是衆所周知的,有將結合ELMAH使用額外的工作。 How to get ELMAH to work with ASP.NET MVC [HandleError] attribute?

2

如果你想處理您的操作,你可以在你的控制器越權「onException的」像這樣的例外:

protected override void OnException(ExceptionContext filterContext) 
{ 
    logging or user notification code here 
} 

你可以把它放在你的BaseController類防止重複

0

trycatch是預期的例外,即你的用戶已經進入了一個文件名,它可能不存在,所以你想趕上FileNotFoundException

對於意外的異常,請使用MvcApplication對象中的Error事件,例如

public class MvcApplication : HttpApplication 
{ 
    protected void Application_Start() 
    { 
     this.Error += MvcApplication_Error; 
     // Other code 
    } 

    private void MvcApplication_Error(object sender, EventArgs e) 
    { 
     Exception exception = this.Server.GetLastError(); 
     // Do logging here. 
    } 
} 

或迪馬建議您在使用

protected override void OnException(ExceptionContext filterContext) 
{ 
    // Do logging here. 
} 

保留,你想趕上意料中事,並可以處理代碼改掉和漁獲控制器級別execption處理。 「通用」錯誤處理只是混淆了基本問題,您將不得不在以後挖掘。