我用我自己的控制器厂在MVC 2应用程序,这似乎是这个标准的代码中绕来绕去:MVC路由和自定义错误页找不到问题
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职位的建议,并试图通过在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
...
);
}
正如我在评论中提到过,这已经清除了许多的错误。现在我遇到的问题是,当我启动应用程序时,我立即给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) at System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath,Type requiredBaseType,HttpContext context,Boolean all owCrossApp) at System.Web.UI.PageHandlerFactory.GetHandlerHelper(HttpContext context,String requestType,VirtualPath virtualPath,String physicalPath) at System.Web.UI.PageHandlerFactory.System.Web.IHttpHandlerFactory2.GetHandler(HttpContext context,String requestType, VirtualPath virtualPath,字符串physicalPath) 在System.Web.HttpApplication.MapHttpHandler(HttpContext的上下文中,字符串的RequestType,VirtualPath路径,字符串pathTranslated,布尔useAppConfig) 在System.Web.HttpApplication.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute () at System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean & Synchronousously completed)
和Web配置部分:
<system.web>
<customErrors mode="On" defaultRedirect="Error.aspx" />
...
</system.web>
错误页面是工作就好之前我做了更改路由。如果我注释掉/ content/images路径 - 应用程序再次工作(虽然我回到了“无法找到路径'{0}'的控制器,或者它没有实现IController”错误)。注释掉favicon或allaspx路由对这个404错误没有任何影响。
任何帮助将不胜感激。
干杯,
Tim。