2012-08-31 37 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处理。 “通用”错误处理只是混淆了基本问题,您将不得不在以后挖掘。