如何有力地捕获Controller / Action Not Found错误

时间:2021-10-05 07:44:09

We're using ASP.NET MVC 5.2.3.

我们正在使用ASP.NET MVC 5.2.3。

What is the best way (most orthogonal/decoupled, simplest) to catch exceptions of the form

捕获表单异常的最佳方法(最正交/解耦,最简单)是什么?

The controller for path '/MissingController/MissingAction' was not found or does not implement IController.

and

A public action method 'MissingAction' was not found on controller 'ExistingController'.

such that they don't reach our last resort error handling in HttpApplication.Application_Error()?

这样他们就不会在HttpApplication.Application_Error()中达到最后的错误处理?

We're aware of a couple of partial solutions:

我们知道几个部分解决方案:

Catching action method not found on controller proposes two solutions to handle the case of a missing action:

在控制器上找不到捕获操作方法提出了两种解决方案来处理缺失操作的情况:

  1. Overriding HandleUnknownAction() in a base controller (downside: all controllers must inherit from that controller).
  2. 覆盖基本控制器中的HandleUnknownAction()(缺点:所有控制器必须从该控制器继承)。

  3. Implementing a custom AsyncControllerActionInvoker and catching HttpException around InvokeAction(), but I'm not convinced about this method as it seems that it would be catching much more than just missing action errors.
  4. 实现一个自定义AsyncControllerActionInvoker并在InvokeAction()周围捕获HttpException,但我不相信这个方法,因为它似乎捕获的不仅仅是缺少动作错误。

http://www.jomendez.com/2016/01/20/how-to-create-custom-controller-factory-asp-net/ proposes to implement a custom controller factory. It is not clear to me how we would redirect to our 404 error page if the controller cannot be found.

http://www.jomendez.com/2016/01/20/how-to-create-custom-controller-factory-asp-net/建议实施自定义控制器工厂。如果找不到控制器,我不清楚如何重定向到404错误页面。

Note that we do provide custom 404 and 500 error pages via Web.config:

请注意,我们通过Web.config提供自定义404和500错误页面:

<system.web>
    <customErrors mode="RemoteOnly" redirectMode="ResponseRewrite" defaultRedirect="~/500.aspx">
        <error statusCode="404" redirect="~/404.aspx"/>
        <error statusCode="500" redirect="~/500.aspx"/>
    </customErrors>
    ...
</system.web>
<system.webServer>
    <httpErrors errorMode="DetailedLocalOnly">
        <remove statusCode="404"/>
        <error statusCode="404" path="404.html" responseMode="File"/>
        <remove statusCode="500"/>
        <error statusCode="500" path="500.html" responseMode="File"/>
    </httpErrors>
    ...
</system.webServer>

but despite this exceptions are still reaching HttpApplication.Application_Error().

但尽管有这些例外,仍然会达到HttpApplication.Application_Error()。

1 个解决方案

#1


0  

We simply ended up doing the following in our HttpApplication.Application_Error():

我们最终在HttpApplication.Application_Error()中完成了以下操作:

var ex = Server.GetLastError();
var httpException = ex as HttpException;

if (httpException == null || httpException.GetHttpCode() != 404)
{
    // Not an HttpException, or HTTP error other than 404.
    // Here: log error, send alert, etc.
}

#1


0  

We simply ended up doing the following in our HttpApplication.Application_Error():

我们最终在HttpApplication.Application_Error()中完成了以下操作:

var ex = Server.GetLastError();
var httpException = ex as HttpException;

if (httpException == null || httpException.GetHttpCode() != 404)
{
    // Not an HttpException, or HTTP error other than 404.
    // Here: log error, send alert, etc.
}