【发布时间】: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