如何在ASP.NET MVC 3应用程序中处理未捕获的异常?

时间:2022-01-09 06:14:09

I want to handle uncaught exceptions in my ASP.NET MVC 3 application, so that I may communicate the error to the user via the application's error view. How do I intercept uncaught exceptions? I'd like to be able to do this globally, not for each controller (although I wouldn't mind knowing how to do this as well).

我想在我的ASP.NET MVC 3应用程序中处理未捕获的异常,以便我可以通过应用程序的错误视图将错误传达给用户。如何拦截未捕获的异常?我希望能够在全球范围内实现这一目标,而不是每个控制器(尽管我不介意知道如何做到这一点)。

5 个解决方案

#1


19  

You can set up a global error filter in Global.asax

您可以在Global.asax中设置全局错误过滤器

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute());
}

The above sets up a default error handler which directs all exceptions to the standard error View. The error view is typed to a System.Web.Mvc.HandleErrorInfo model object which exposes the exception details.

上面设置了一个默认的错误处理程序,它将所有异常定向到标准错误V​​iew。错误视图键入System.Web.Mvc.HandleErrorInfo模型对象,该对象公开异常详细信息。

You also need to turn on custom errors in the web.config to see this on your local machine.

您还需要在web.config中打开自定义错误,才能在本地计算机上看到此错误。

<customErrors mode="On"/>

You can also define multiple filters for specific error types:

您还可以为特定错误类型定义多个过滤器:

filters.Add(new HandleErrorAttribute
{
    ExceptionType = typeof(SqlException),
    View = "DatabaseError",
    Order = 1
});

/* ...other error type handlers here */

filters.Add(new HandleErrorAttribute()); // default handler

Note that HandleErrorAttribute will only handle errors that happen inside of the MVC pipeline (i.e. 500 errors).

请注意,HandleErrorAttribute仅处理在MVC管道内发生的错误(即500个错误)。

#2


10  

you can use HandleErrorAttribute filters,

你可以使用HandleErrorAttribute过滤器,

[ErrorHandler(ExceptionType = typeof(Exception), View = "UnhandledError", Order = 1)]
 public abstract class BaseController : Controller

        {
    }

basically you can have this on top of a base controller and define the UnhandledError.cshtml in the Shared views folder.

基本上你可以在基本控制器上安装它,并在共享视图文件夹中定义UnhandledError.cshtml。

And if you want to log the unhandled errors before you show the error message then you can extend the HandleErrorAttribute class and put the logic to do the logging inside the OnException method.

如果要在显示错误消息之前记录未处理的错误,则可以扩展HandleErrorAttribute类并将逻辑放在OnException方法内部进行日志记录。

public class MyErrorHandlerAttribute : HandleErrorAttribute
    {


        public override void OnException(ExceptionContext exceptionContext)
        {
            Logger.Error(exceptionContext.Exception.Message,exceptionContext.Exception);
            base.OnException(exceptionContext);
        }
    }

#3


4  

For completeness sake, there is also the Application_Error handler in Global.asax.

为了完整起见,Global.asax中还有Application_Error处理程序。

#4


3  

Global Error Handling

全局错误处理

  1. Add in web.config

    添加web.config

    <customErrors mode="On"/>
  2. Error will be displayed on Error.cshtml which is resides in shared folder

    Error.cshtml上将显示错误,该错误位于共享文件夹中

  3. Change in Error.cshtml
  4. 更改Error.cshtml
    @model System.Web.Mvc.HandleErrorInfo

    @{
        ViewBag.Title = "Error"; }


    <h2>
        <p>Sorry, an error occurred while processing your request.</p>
        <p>Controller Name: @Model.ControllerName</p>
        <p>Action Name : @Model.ActionName</p>
        <p>Message: @Model.Exception.Message</p> </h2>

#5


2  

in order to make this work I followed the following blog post and then make the following addition to both Web.config files (the root one and the one in the Views folder) inside the <system.web> node:

为了完成这项工作,我按照以下博客文章,然后在 节点内对Web.config文件(根文件和Views文件夹中的文件)进行以下添加:

  ...
  <system.web>
    <customErrors mode="On"/>
    ...

Hope it helps...

希望能帮助到你...

#1


19  

You can set up a global error filter in Global.asax

您可以在Global.asax中设置全局错误过滤器

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute());
}

The above sets up a default error handler which directs all exceptions to the standard error View. The error view is typed to a System.Web.Mvc.HandleErrorInfo model object which exposes the exception details.

上面设置了一个默认的错误处理程序,它将所有异常定向到标准错误V​​iew。错误视图键入System.Web.Mvc.HandleErrorInfo模型对象,该对象公开异常详细信息。

You also need to turn on custom errors in the web.config to see this on your local machine.

您还需要在web.config中打开自定义错误,才能在本地计算机上看到此错误。

<customErrors mode="On"/>

You can also define multiple filters for specific error types:

您还可以为特定错误类型定义多个过滤器:

filters.Add(new HandleErrorAttribute
{
    ExceptionType = typeof(SqlException),
    View = "DatabaseError",
    Order = 1
});

/* ...other error type handlers here */

filters.Add(new HandleErrorAttribute()); // default handler

Note that HandleErrorAttribute will only handle errors that happen inside of the MVC pipeline (i.e. 500 errors).

请注意,HandleErrorAttribute仅处理在MVC管道内发生的错误(即500个错误)。

#2


10  

you can use HandleErrorAttribute filters,

你可以使用HandleErrorAttribute过滤器,

[ErrorHandler(ExceptionType = typeof(Exception), View = "UnhandledError", Order = 1)]
 public abstract class BaseController : Controller

        {
    }

basically you can have this on top of a base controller and define the UnhandledError.cshtml in the Shared views folder.

基本上你可以在基本控制器上安装它,并在共享视图文件夹中定义UnhandledError.cshtml。

And if you want to log the unhandled errors before you show the error message then you can extend the HandleErrorAttribute class and put the logic to do the logging inside the OnException method.

如果要在显示错误消息之前记录未处理的错误,则可以扩展HandleErrorAttribute类并将逻辑放在OnException方法内部进行日志记录。

public class MyErrorHandlerAttribute : HandleErrorAttribute
    {


        public override void OnException(ExceptionContext exceptionContext)
        {
            Logger.Error(exceptionContext.Exception.Message,exceptionContext.Exception);
            base.OnException(exceptionContext);
        }
    }

#3


4  

For completeness sake, there is also the Application_Error handler in Global.asax.

为了完整起见,Global.asax中还有Application_Error处理程序。

#4


3  

Global Error Handling

全局错误处理

  1. Add in web.config

    添加web.config

    <customErrors mode="On"/>
  2. Error will be displayed on Error.cshtml which is resides in shared folder

    Error.cshtml上将显示错误,该错误位于共享文件夹中

  3. Change in Error.cshtml
  4. 更改Error.cshtml
    @model System.Web.Mvc.HandleErrorInfo

    @{
        ViewBag.Title = "Error"; }


    <h2>
        <p>Sorry, an error occurred while processing your request.</p>
        <p>Controller Name: @Model.ControllerName</p>
        <p>Action Name : @Model.ActionName</p>
        <p>Message: @Model.Exception.Message</p> </h2>

#5


2  

in order to make this work I followed the following blog post and then make the following addition to both Web.config files (the root one and the one in the Views folder) inside the <system.web> node:

为了完成这项工作,我按照以下博客文章,然后在 节点内对Web.config文件(根文件和Views文件夹中的文件)进行以下添加:

  ...
  <system.web>
    <customErrors mode="On"/>
    ...

Hope it helps...

希望能帮助到你...