如何创建应用程序范围的404错误页面?

时间:2010-08-02 01:58:25

标签: asp.net-mvc-2 http-status-code-404

在我的ASP.Net MVC2中,如何创建宽404页面?

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

1 个答案:

答案 0 :(得分:2)

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

<customErrors mode="On|RemoteOnly" defaultRedirect="/error/problem">
    <error statusCode="404" redirect="/error/notfound"/>
    <error statusCode="500" redirect="/error/problem"/>
</customErrors>

和/或创建一个错误处理控制器并将其用于一个catchall路由:

ErrorController.cs:

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();
    }
}

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" }
);