3

我试图在我的 MVC 应用程序中“捕获所有”500 和 404 错误,但我似乎无法掌握需要什么,即使在阅读了所有文章和问题之后也是如此。

Web.config(这允许 500 错误去~/Views/Shared/Error.cshtml):

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

我已经设置了HomeController抛出错误来测试上述设置:

public ActionResult Index()
{
    //Testing errors
    throw new Exception("Exception");

    return View();
}

在我的Global.asax.cs中,我有以下记录 500 错误:

protected void Application_Error()
{
    var ex = Server.GetLastError();

    //Custom ExceptionLog
    new ExceptionLogHelper().Add("Application_Error", Response.Status, ex);
}

现在对于 404 错误:

在我的RouteConfig.cs中,我有以下路线,但似乎无法捕获所有 404:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Error",
            url: "Error/{code}",
            defaults: new { controller = "Error", action = "Index", code = UrlParameter.Optional }
        );

        //routes.MapRoute(
        //  name: "Controllers",
        //  url: "{controller}/{action}/{id}",
        //  defaults: new { controller = "Error", action = "Index", code = UrlParameter.Optional }
        //);

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

        //Keep at bottom
        routes.MapRoute("CatchAll", "{*url}", new { controller = "Error", action = "Index", name = "no-route-found", code = "404" });

    }
}

CatchAll在底部做得很好,可以捕捉到与前面的路线不匹配的所有东西。

我有很多测试场景,但困扰我的是以下 UrlParameter:

http://localhost:64275/does/not/exist/

上面的网址本质上是http://localhost:64275/{controller}/{action}/{id}

我没有一个名为 的控制器does,我认为如果没有匹配的控制器,它defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }会默认为Home具有 Action 的控制器。Index

另一个有效的例子:

http://localhost:64275/a/a/a/a/ (because it has 4 parts, not 3 or less)

有人可以解释我可能会出错的地方吗?......我不明白什么?

我是否应该实现这样的东西:.Net MVC Routing Catchall not working (Darin Dimitrov's answer)

protected void Application_Error(object sender, EventArgs e)
{
    Exception exception = Server.GetLastError();
    HttpException httpException = exception as HttpException;
    if (httpException != null)
    {
        RouteData routeData = new RouteData();
        routeData.Values.Add("controller", "Error");
        routeData.Values.Add("action", "HttpError500");

            if (httpException.GetHttpCode() == 404)
            {
                routeData.Values["action"] = "HttpError404";
            }

        Server.ClearError();
        Response.Clear();
        IController errorController = new ErrorController();
        errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
    }
}
4

1 回答 1

2

您是正确的,因为在这种情况下正在命中默认路由http://localhost:64275/does/not/exist/。但是,路由没有内置任何昂贵的反射调用来确保控制器在尝试创建之前存在。

但是,您可以通过制作自己的自定义来进行干预,IControllerFactory当 MVC 无法找到路由中指定的控制器实例时,该自定义知道该怎么做。

NotFoundControllerFactory.cs

public class NotFoundControllerFactory : DefaultControllerFactory
{
    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        // If controller not found it will be null, so we want to take control
        // of the request and send it to the ErrorController.NotFound method.
        if (controllerType == null)
        {
            requestContext.RouteData.Values["action"] = "NotFound";
            requestContext.RouteData.Values["controller"] = "Error";
            return base.GetControllerInstance(requestContext, typeof(ErrorController));
        }

        return base.GetControllerInstance(requestContext, controllerType);
    }
}

DefaultControllerFactory接受控制器作为字符串,然后尝试将控制器名称解析为类型。如果不能,则类型是null它调用的时间GetControllerInstance(RequestContext, Type)。所以,我们所要做的就是检查类型是否是null,然后重写我们的请求,以便实例化ErrorController并调用NotFoundaction 方法。

用法

您只需要在启动时向 MVC 注册控制器工厂。

ControllerBuilder.Current.SetControllerFactory(new NotFoundControllerFactory());

然后要将所有内容联系在一起,您的NotFound操作方法应将状态代码设置为 404。

public class ErrorController : Controller
{
    public ActionResult NotFound()
    {
        Response.StatusCode = (int)System.Net.HttpStatusCode.NotFound;
        Response.StatusDescription = "404 Not Found";

        return View();
    }
}

至于动作方法,如果控制器上不存在动作,MVC会以 404 Not Found 响应。

于 2018-02-14T03:59:08.490 回答