首页 文章

自定义ASP.NET MVC 404错误页面的路由

提问于
浏览
109

当有人键入不调用ASP.NET MVC中的有效操作或控制器的URL而不显示通用的“未找到资源”ASP.NET错误时,我试图创建自定义HTTP 404错误页面 .

我不想使用web.config来处理这个问题 .

是否有任何类型的路由魔法可以捕获任何无效的URL?

Update: 我尝试了给出的答案,但是我仍然得到了丑陋的"Resource Not Found"消息 .

Another update: 好的,RC1显然发生了一些变化 . 我甚至试图在 HttpException 上专门捕获404,它仍然只是给了我"Resource Not Found"页面 .

我甚至使用过MvcContrib的资源功能,没有 - 同样的问题 . 有任何想法吗?

9 回答

  • 9

    只需在路由表的末尾添加catch all route并显示您想要的任何页面 .

    见:How can i make a catch all route to handle '404 page not found' queries for ASP.NET MVC?

  • 15

    我试图在 生产环境 服务器上启用自定义错误3个小时,似乎我找到了最终解决方案如何在没有任何路由的ASP.NET MVC中执行此操作 .

    要在ASP.NET MVC应用程序中启用自定义错误,我们需要(IIS 7):

    • system.web 部分的Web配置中配置自定义页面:
    <customErrors mode="RemoteOnly"  defaultRedirect="~/error">
        <error statusCode="404" redirect="~/error/Error404" />
        <error statusCode="500" redirect="~/error" />
    </customErrors>
    

    RemoteOnly 表示在本地网络上您将看到真正的错误(在开发过程中非常有用) . 我们还可以为任何错误代码重写错误页面 .

    • 设置魔术响应参数和响应状态代码(在错误处理模块或错误句柄属性中)
    HttpContext.Current.Response.StatusCode = 500;
      HttpContext.Current.Response.TrySkipIisCustomErrors = true;
    
    • system.webServer 部分的网络配置中设置另一个魔术设置:
    <httpErrors errorMode="Detailed" />
    

    这是我发现的最后一件事,在此之后我可以在 生产环境 服务器上看到自定义错误 .

  • 27

    我通过创建一个返回本文中视图的ErrorController来使我的错误处理工作 . 我还必须在global.asax中添加“Catch All”到路由 .

    如果它不在Web.config中,我无法看到它将如何到达任何这些错误页面 . ?我的Web.config必须指定:

    customErrors mode="On" defaultRedirect="~/Error/Unknown"
    

    然后我还补充说:

    error statusCode="404" redirect="~/Error/NotFound"
    
  • 18

    Source

    NotFoundMVC - 只要在ASP.NET MVC3应用程序中找不到控制器,操作或路由,就会提供用户友好的404页面 . 将呈现名为NotFound的视图,而不是默认的ASP.NET错误页面 .

    您可以使用以下命令通过nuget添加此插件:Install-Package NotFoundMvc

    NotFoundMvc在Web应用程序启动期间自动安装 . 它处理ASP.NET MVC通常抛出404 HttpException的所有不同方式 . 这包括缺少控制器,动作和路线 .

    Step by Step Installation Guide :

    1 - 右键单击您的项目并选择Manage Nuget Packages ...

    2 - 搜索 NotFoundMvc 并安装它 .
    enter image description here

    3 - 安装完成后,将向项目中添加两个文件 . 如下面的屏幕截图所示 .

    enter image description here

    4 - 打开Views / Shared中新添加的NotFound.cshtml,并根据您的意愿修改它 . 现在运行应用程序并输入一个不正确的URL,您将看到一个用户友好的404页面 .

    enter image description here

    没有更多,用户会收到错误消息,如 Server Error in '/' Application. The resource cannot be found.

    希望这可以帮助 :)

    P.S:感谢Andrew Davey做了这么棒的插件 .

  • 0

    在web.config中尝试此操作以替换IIS错误页面 . 这是我猜的最佳解决方案,它也会发出正确的状态代码 .

    <system.webServer>
      <httpErrors errorMode="Custom" existingResponse="Replace">
        <remove statusCode="404" subStatusCode="-1" />
        <remove statusCode="500" subStatusCode="-1" />
        <error statusCode="404" path="Error404.html" responseMode="File" />
        <error statusCode="500" path="Error.html" responseMode="File" />
      </httpErrors>
    </system.webServer>
    

    更多信息来自Tipila - Use Custom Error Pages ASP.NET MVC

  • 41

    此解决方案不需要web.config文件更改或catch-all路由 .

    首先,创建一个这样的控制器;

    public class ErrorController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Title = "Regular Error";
            return View();
        }
    
        public ActionResult NotFound404()
        {
            ViewBag.Title = "Error 404 - File not Found";
            return View("Index");
        }
    }
    

    然后在“Views / Error / Index.cshtml”下创建视图;

    @{
          Layout = "~/Views/Shared/_Layout.cshtml";
      }                     
      <p>We're sorry, page you're looking for is, sadly, not here.</p>
    

    然后在Global asax文件中添加以下内容,如下所示:

    protected void Application_Error(object sender, EventArgs e)
    {
            // Do whatever you want to do with the error
    
            //Show the custom error page...
            Server.ClearError(); 
            var routeData = new RouteData();
            routeData.Values["controller"] = "Error";
    
            if ((Context.Server.GetLastError() is HttpException) && ((Context.Server.GetLastError() as HttpException).GetHttpCode() != 404))
            {
                routeData.Values["action"] = "Index";
            }
            else
            {
                // Handle 404 error and response code
                Response.StatusCode = 404;
                routeData.Values["action"] = "NotFound404";
            } 
            Response.TrySkipIisCustomErrors = true; // If you are using IIS7, have this line
            IController errorsController = new ErrorController();
            HttpContextWrapper wrapper = new HttpContextWrapper(Context);
            var rc = new System.Web.Routing.RequestContext(wrapper, routeData);
            errorsController.Execute(rc);
    
            Response.End();
    }
    

    如果在执行此操作后仍然出现自定义IIS错误页面,请确保在Web配置文件中注释掉(或清空)以下部分:

    <system.web>
       <customErrors mode="Off" />
    </system.web>
    <system.webServer>   
       <httpErrors>     
       </httpErrors>
    </system.webServer>
    
  • 100

    如果你在MVC 4工作,你可以看this解决方案,它对我有用 .

    将以下Application_Error方法添加到我的 Global.asax

    protected void Application_Error(object sender, EventArgs e)
    {
        Exception exception = Server.GetLastError();
        Server.ClearError();
    
        RouteData routeData = new RouteData();
        routeData.Values.Add("controller", "Error");
        routeData.Values.Add("action", "Index");
        routeData.Values.Add("exception", exception);
    
        if (exception.GetType() == typeof(HttpException))
        {
            routeData.Values.Add("statusCode", ((HttpException)exception).GetHttpCode());
        }
        else
        {
            routeData.Values.Add("statusCode", 500);
        }
    
        IController controller = new ErrorController();
        controller.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
        Response.End();
    

    控制器本身非常简单:

    public class ErrorController : Controller
    {
        public ActionResult Index(int statusCode, Exception exception)
        {
            Response.StatusCode = statusCode;
            return View();
        }
    }
    

    查看Mvc4CustomErrorPage at GitHub的完整源代码 .

  • 5

    我遇到了同样的问题,您需要做的是,不必在Views文件夹的web.config文件中添加customErrors属性,而是必须将它添加到项目根文件夹的web.config文件中

  • 0

    这是真正的答案,允许在一个地方完全自定义错误页面 . 无需修改web.config或创建单独的代码 .

    也适用于MVC 5 .

    将此代码添加到控制器:

    if (bad) {
                Response.Clear();
                Response.TrySkipIisCustomErrors = true;
                Response.Write(product + I(" Toodet pole"));
                Response.StatusCode = (int)HttpStatusCode.NotFound;
                //Response.ContentType = "text/html; charset=utf-8";
                Response.End();
                return null;
            }
    

    基于http://www.eidias.com/blog/2014/7/2/mvc-custom-error-pages

相关问题