【问题标题】:MVC Routing and Custom Error Page Not Found ProblemMVC 路由和自定义错误页面未找到问题
【发布时间】:2011-07-06 01:03:01
【问题描述】:

我在 MVC 2 应用程序中使用我自己的控制器工厂,而这个标准代码似乎正在流传:

    protected override IController GetControllerInstance(RequestContext reqContext, Type controllerType)
    {
        ...

        // Error handling
        if (controllerType == null)
        {
            throw new HttpException(
                404, String.Format(
                    "The controller for path '{0}' could not be found" +
                    " or it does not implement IController.",
                    reqContext.HttpContext.Request.Path
                    )
                );
        }

        ...
    }

我遇到的问题是其他人问过的问题,其中某些资源被此错误处理逻辑捕获,而我的 ELMAH 已经填充了数百个毫无意义的错误。更烦人的是,不得不一直按 F5 键错误会使调试变得很痛苦。这些资源是:favicon.ico、Error.aspx(我的自定义错误页面)和特定文件夹中的一些图像。

所以我听从this post的建议,并尝试通过忽略global.asax中的路由来解决问题:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.IgnoreRoute("elmah.axd");
        routes.IgnoreRoute("/Content/images/{*pathinfo}"); // This has succesfully rid me of a bunch of errors
        routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.([iI][cC][oO]|[gG][iI][fF])(/.*)?" }); // This has successfully rid me of the favicon error
        routes.IgnoreRoute("{*allaspx}", new { allaspx = @"(.*/)?.aspx(/.*)?" }); // NOT SURE OF THIS ONE

        ...

        );
    }

正如我在 cmets 中提到的,这已经清除了许多错误。我现在遇到的问题是,当我启动应用程序时,我立即给出了 404 死屏,告诉我找不到我的自定义错误页面。错误详情:

System.Web.HttpException (0x80004005):文件“/Error.aspx”不存在。 在 System.Web.UI.Util.CheckVirtualFileExists(VirtualPath virtualPath) 在 System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath,布尔 noBuild,布尔allowCrossApp,布尔allowBuildInPrecompile,布尔throwIfNotFound,布尔ensureIsUpToDate) 在 System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext 上下文,VirtualPath virtualPath,布尔 noBuild,布尔allowCrossApp,布尔allowBuildInPrecompile,布尔throwIfNotFound,布尔ensureIsUpToDate) 在 System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath,HttpContext 上下文,布尔 allowCrossApp,布尔 throwIfNotFound) 在 System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath,类型 requiredBaseType,HttpContext 上下文,布尔 allowCrossApp) 在 System.Web.UI.PageHandlerFactory.GetHandlerHelper(HttpContext 上下文,字符串 requestType,VirtualPath virtualPath,字符串物理路径) 在 System.Web.UI.PageHandlerFactory.System.Web.IHttpHandlerFactory2.GetHandler(HttpContext 上下文,字符串 requestType,VirtualPath virtualPath,字符串物理路径) 在 System.Web.HttpApplication.MapHttpHandler(HttpContext 上下文,字符串 requestType,VirtualPath 路径,String pathTranslated,布尔 useAppConfig) 在 System.Web.HttpApplication.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 在 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

还有网络配置部分:

<system.web>
   <customErrors mode="On" defaultRedirect="Error.aspx" />

   ...
</system.web>

在我对路由进行更改之前,错误页面运行良好。如果我注释掉 /content/images 路径 - 应用程序再次运行(尽管我回到“找不到路径'{0}' 的控制器或它没有实现 IController”错误)。注释掉 favicon 或 allaspx 路由对这个 404 错误没有任何改变。

任何帮助将不胜感激。

干杯,

提姆。

【问题讨论】:

    标签: asp.net-mvc-2 asp.net-mvc-routing


    【解决方案1】:

    问题是错误页面设置错误。

    我回到最开始,this 教程。我没有 ErrorController 并且正在使用 HandleError 属性。按照此链接中的说明修复了错误页面的问题。忽略路线的答案适用于网站图标。其他文件确实是丢失的文件。

    【讨论】:

      【解决方案2】:

      我们得到相同的错误信息,但问题不同,我们传递的可选参数包含/ 字符,这导致应用程序寻找不同的路由。所以请确认您的参数不包含/ 字符。

      【讨论】:

        猜你喜欢
        • 2014-03-07
        • 2010-10-07
        • 1970-01-01
        • 2015-10-04
        • 1970-01-01
        • 1970-01-01
        • 2020-05-26
        • 2015-09-10
        • 1970-01-01
        相关资源
        最近更新 更多