Asp.net mvc 在ASP.NET MVC中,这是正确的异常处理方式吗

Asp.net mvc 在ASP.NET MVC中,这是正确的异常处理方式吗,asp.net-mvc,exception-handling,Asp.net Mvc,Exception Handling,我读过关于ASP.NET MVC中异常处理的文章。我想通过简单的介绍来确保我做得对。请任何人发表评论 如有必要,捕获控制器操作中的异常 [HttpPost] public ActionResult Insert() { try { } catch { //ModelState.Error -> display error msg to the user. } } 重写basecontroller中controller的“

我读过关于ASP.NET MVC中异常处理的文章。我想通过简单的介绍来确保我做得对。请任何人发表评论

  • 如有必要,捕获控制器操作中的异常

    [HttpPost]
    public ActionResult Insert()
    {
        try
        {
    
        }
        catch
        {
            //ModelState.Error -> display error msg to the user.
        }
    }
    
  • 重写basecontroller中controller的“OneException”方法,并“记录”步骤1中引发的异常和其他MVC异常

  • 在应用程序错误中记录全局异常


  • 如果您想在操作中处理异常,可以在控制器中重写“OneException”,如下所示:

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

    您可以将其放在BaseController类中以防止重复

    如果您想在操作中处理异常,可以在控制器中重写“OneException”,如下所示:

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

    您可以将它放在BaseController类中以防止重复

    我绝对推荐ELMaH,而不是自己编写此代码,也可以通过Log4Net为您的MVC应用程序编写此代码。我个人避免任何异常处理,除非我有一个特定的功能响应。这样,我就不会“吃”任何应用程序范围的工具(如ELMaH)为我优雅地处理的错误

    ELMaH也有很好的内置web报告,并且有专门针对ELMaH的第三方工具可以提供统计信息,例如最常见的错误

    您可以从自定义错误重定向开始

    <customErrors defaultRedirect="~/site/error" mode="RemoteOnly">
      <error statusCode="404" redirect="~/site/notfound" />
    </customErrors>
    
    …由帮助访问者向您获取特定错误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>
    

    HandleErrorAttribute是一个很好的特性,但众所周知,将它与ELMaH结合使用需要额外的工作

    我绝对推荐ELMaH,而不是自己编写这段代码,也可以通过Log4Net为您的MVC应用程序编写这段代码。我个人避免任何异常处理,除非我有一个特定的功能响应。这样,我就不会“吃”任何应用程序范围的工具(如ELMaH)为我优雅地处理的错误

    ELMaH也有很好的内置web报告,并且有专门针对ELMaH的第三方工具可以提供统计信息,例如最常见的错误

    您可以从自定义错误重定向开始

    <customErrors defaultRedirect="~/site/error" mode="RemoteOnly">
      <error statusCode="404" redirect="~/site/notfound" />
    </customErrors>
    
    …由帮助访问者向您获取特定错误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>
    

    HandleErrorAttribute是一个很好的特性,但众所周知,将它与ELMaH结合使用需要额外的工作

    try
    catch
    用于预期的异常,即您的用户输入了文件名,但文件名可能不存在,因此您希望捕获
    FileNotFoundException

    对于意外异常,请使用MVCAPApplication对象中的错误事件,例如

    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.
        }
    }
    
    或者,正如Dima所建议的,您可以使用

    protected override void OnException(ExceptionContext filterContext)
    {
       // Do logging here.
    }
    
    在您希望捕获预期和可以处理的内容的代码上保持try和catch。
    “通用”错误处理只是混淆了潜在的问题,稍后您将不得不挖掘。

    try
    catch
    用于预期的异常,即您的用户输入了一个文件名,但它可能不存在,因此您希望捕获
    FileNotFoundException

    对于意外异常,请使用MVCAPApplication对象中的错误事件,例如

    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.
        }
    }
    
    或者,正如Dima所建议的,您可以使用

    protected override void OnException(ExceptionContext filterContext)
    {
       // Do logging here.
    }
    
    在您希望捕获预期和可以处理的内容的代码上保持try和catch。
    “通用”错误处理只是混淆了潜在的问题,您以后必须对此进行挖掘。

    看看ELMAHYeah之类的东西,我计划使用log4net,但还没有决定。为什么要重新发明轮子?我承认知道它是如何工作的很好,但是当你明白了,除非它不做你想要的事情,否则自己编写它没有真正的意义,而且log4net的目标与elmah稍有不同,我认为elmah是更意外的异常日志,log4net记录应用程序事件/已知错误,我计划使用log4net,但尚未决定。为什么要重新发明轮子?我承认知道它是如何工作的很好,但是在你理解了除非它不做你想做的事情,否则自己编写它没有真正的意义之后,log4net的目标与elmah稍有不同,我认为elmah是更意外的异常日志,而log4net是记录应用程序事件/已知错误