Asp.net mvc 2 如何创建应用程序范围的404错误页?

Asp.net mvc 2 如何创建应用程序范围的404错误页?,asp.net-mvc-2,http-status-code-404,Asp.net Mvc 2,Http Status Code 404,在我的ASP.NETMVC2中,如何创建宽404页面 意思是每次有人试图进入一个不存在的视图/页面时,他都会被重定向到我选择的错误页面?使用标准ASP.NET错误页面(在web.config中激活): global.asax: // This route catches all urls that do not match any of the previous routes. // So if you registered the standard routes, somthing like

在我的ASP.NETMVC2中,如何创建宽404页面


意思是每次有人试图进入一个不存在的视图/页面时,他都会被重定向到我选择的错误页面?

使用标准ASP.NET错误页面(在web.config中激活):

global.asax:

// This route catches all urls that do not match any of the previous routes.
// So if you registered the standard routes, somthing like "/foo/bar/baz" will
// match the "{controller}/{action}/{id}" route, even if no FooController exists
routes.MapRoute(
     "Catchall",
     "{*catchall}",
     new { controller = "Error", action = "NotFound" }
);

我已经添加了routes.MapRoute(),但它不起作用。你知道为什么吗?我已经创建了控制器和视图。Catchall路由只捕获与任何其他路由不匹配的路由。因此,如果您有标准路由“{controller}/{action}/{id}”,那么每个看起来像“/foo/bar/baz”的Url都将匹配该路由,即使不存在FooController。要捕获所有缺少的控制器/操作,最简单的解决方案是使用ASP.NET自定义错误。您可以为错误页面使用静态文件,也可以为此创建控制器/操作(使用上面的示例将您重定向到ErrorController并调用NotFound操作)。我编辑了我的答案,将其包括在内。有关此主题的更多详细信息,请查看此问题:
public class ErrorController : Controller
{
    public ActionResult NotFound(string aspxerrorpath)
    {
        // probably you'd like to log the missing url. "aspxerrorpath" is automatically added to the query string when using standard ASP.NET custom errors
        // _logger.Log(aspxerrorpath); 
        return View();
    }
}
// This route catches all urls that do not match any of the previous routes.
// So if you registered the standard routes, somthing like "/foo/bar/baz" will
// match the "{controller}/{action}/{id}" route, even if no FooController exists
routes.MapRoute(
     "Catchall",
     "{*catchall}",
     new { controller = "Error", action = "NotFound" }
);