处理ASP.NET MVC中的路由错误

时间:2021-05-20 04:12:49

I understand how to set up my own routes, but how does one handle routes that fall through the cracks of the routing table? I mean, I guess the default {controller}/{action}/{id} route could be a generic catchall, but I'm not sure if that's the way to go. I like letting my users know they've requested data/a 'page' that doesn't exist.

我理解如何设置自己的路由,但是如何处理路由表中的路径呢?我的意思是,我猜默认的{controller} / {action} / {id}路由可能是一个通用的catchall,但我不确定这是否可行。我喜欢让我的用户知道他们已经请求了数据/不存在的“页面”。

Is this where the [HandleError] filter comes in? How does that work, exactly?

这是[HandleError]过滤器的来源吗?这究竟是如何工作的?

2 个解决方案

#1


7  

If your route is not found, you want to handle it as a normal HTTP 404 error.

如果找不到您的路由,您希望将其作为正常的HTTP 404错误处理。

If you only add the [HandleError] attribute to your class or action, MVC will look for an Error view in your views folder.

如果只将[HandleError]属性添加到类或操作中,MVC将在视图文件夹中查找错误视图。

You could also add an ErrorController or even a static page and add this to your Web.config:

您还可以添加ErrorController甚至静态页面并将其添加到Web.config:

<customErrors mode="On" >
    <error statusCode="404" redirect="/Error/PageNotFound/" />
</customErrors>

Or you could handle the HTTP 404 in your Global.asax.cs and route to an ErrorController programmatically. That's how I generally do it:

或者您可以在Global.asax.cs中处理HTTP 404并以编程方式路由到ErrorController。这就是我通常这样做的方式:

protected void Application_Error(object sender, EventArgs e)
{
    var ex = Server.GetLastError().GetBaseException();

    var routeData = new RouteData();

    if (ex.GetType() == typeof(HttpException))
    {
        var httpException = (HttpException)ex;

        switch (httpException.GetHttpCode())
        {
            case 404:
                routeData.Values.Add("action", "PageNotFound");
                break;
            default:
                routeData.Values.Add("action", "GeneralError");
                break;
        }
    }
    else
    {
        routeData.Values.Add("action", "GeneralError");
    }

    routeData.Values.Add("controller", "Error");
    routeData.Values.Add("error", ex);

    IController errorController = new ErrorController();
    errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
}

#2


4  

You can define a route like this:

您可以定义这样的路线:

routes.MapRoute(
                "PageNotFound",
                "{*catchall}",
                new { controller = "Home", action = "PageNotFound" }
                );

Than make an action in a controller like this:

比在这样的控制器中执行操作:

        public ActionResult PageNotFound()
        {
            ViewBag.Message = "Sorry, the page you requested does not exist.";
            return View();
        }

This route sould be added last, that way it will catch any request that can't be mapped.

此路由最后可以添加,这样它将捕获任何无法映射的请求。

HandleError attribute is used to catch exceptions that may occure within controller actions.

HandleError属性用于捕获控制器操作中可能发生的异常。

#1


7  

If your route is not found, you want to handle it as a normal HTTP 404 error.

如果找不到您的路由,您希望将其作为正常的HTTP 404错误处理。

If you only add the [HandleError] attribute to your class or action, MVC will look for an Error view in your views folder.

如果只将[HandleError]属性添加到类或操作中,MVC将在视图文件夹中查找错误视图。

You could also add an ErrorController or even a static page and add this to your Web.config:

您还可以添加ErrorController甚至静态页面并将其添加到Web.config:

<customErrors mode="On" >
    <error statusCode="404" redirect="/Error/PageNotFound/" />
</customErrors>

Or you could handle the HTTP 404 in your Global.asax.cs and route to an ErrorController programmatically. That's how I generally do it:

或者您可以在Global.asax.cs中处理HTTP 404并以编程方式路由到ErrorController。这就是我通常这样做的方式:

protected void Application_Error(object sender, EventArgs e)
{
    var ex = Server.GetLastError().GetBaseException();

    var routeData = new RouteData();

    if (ex.GetType() == typeof(HttpException))
    {
        var httpException = (HttpException)ex;

        switch (httpException.GetHttpCode())
        {
            case 404:
                routeData.Values.Add("action", "PageNotFound");
                break;
            default:
                routeData.Values.Add("action", "GeneralError");
                break;
        }
    }
    else
    {
        routeData.Values.Add("action", "GeneralError");
    }

    routeData.Values.Add("controller", "Error");
    routeData.Values.Add("error", ex);

    IController errorController = new ErrorController();
    errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
}

#2


4  

You can define a route like this:

您可以定义这样的路线:

routes.MapRoute(
                "PageNotFound",
                "{*catchall}",
                new { controller = "Home", action = "PageNotFound" }
                );

Than make an action in a controller like this:

比在这样的控制器中执行操作:

        public ActionResult PageNotFound()
        {
            ViewBag.Message = "Sorry, the page you requested does not exist.";
            return View();
        }

This route sould be added last, that way it will catch any request that can't be mapped.

此路由最后可以添加,这样它将捕获任何无法映射的请求。

HandleError attribute is used to catch exceptions that may occure within controller actions.

HandleError属性用于捕获控制器操作中可能发生的异常。