I am using my own controller factory in an MVC 2 app, and this standard code which seems to be going around:
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
)
);
}
...
}
The problem I was having is one others have asked about, where certain resources where being caught by this error handling logic and my ELMAH has been getting populated with hundreds of pointless errors. More annoyingly, it makes debugging painful to have to keep F5-ing the errors. These resources are: favicon.ico, Error.aspx (my custom error page), and some images in a particular folder.
So I followed the advice of this post, and attempted to solve the issues through ignoring routes in the 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
...
);
}
As I've mentioned in the comments, this has cleared up many of the errors. The problem I now have is that when I start the application, I'm immediatley given a 404 screen of death, telling me my custom error page cannot be found. The error details:
System.Web.HttpException (0x80004005): The file '/Error.aspx' does not exist. at System.Web.UI.Util.CheckVirtualFileExists(VirtualPath virtualPath) at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate) at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate) at System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean throwIfNotFound) at System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath, Type requiredBaseType, HttpContext context, Boolean allowCrossApp) 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, String physicalPath) at System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig) at System.Web.HttpApplication.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
And the web config section:
<system.web>
<customErrors mode="On" defaultRedirect="Error.aspx" />
...
</system.web>
The error page was working just fine before I made the changes to the routing. If I comment out the /content/images path - the app works again (though I'm back to "The controller for path '{0}' could not be found or it does not implement IController" errors). Commenting out the favicon or allaspx route has no change on this 404 error.
Any help would be greatly appreciated.
Cheers,
Tim.