I have this as my DefaultControllerFactory override:
public class StructureMapControllerFactory : DefaultControllerFactory
{
protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, System.Type controllerType)
{
if (controllerType == null) return base.GetControllerInstance(requestContext, controllerType);
return (IController)ObjectFactory.GetInstance(controllerType);
}
}
My Global.asax looks like this:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("favicon.ico");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
开发者_如何学Python);
routes.MapRoute(
"404-PageNotFound",
"{*url}",
new { controller = "ErrorController", action = "PageNotFound" }
);
}
When I go to mydomain.com/asd/asd/asd/asd/asd/asd
it throws a 404 exception and then looks in the customErrors
node in the web.config to decide what to do.
I'm not sure whether this is right or not because I would have thought my routes would have handled this and not the customErrors
.
I think that problem is in "404-PageNotFound" route declaration. If you want this to be handled by ErrorController, you should specify "Error" as route value, not "ErrorController", as asp.net mvc controller naming convention states. In your case, probably asp.net default 404 error is rendered because it cannot navigate to "ErrorController"
routes.MapRoute(
"404-PageNotFound",
"{*url}",
new { controller = "Error", action = "PageNotFound" }
);
Your route "{controller}/{action}/{id}"
will only handle mydomain.com/asd/asd/asd
if you have such a deep folder structure, then you need to add routes that will handle all the folders.
In order to catch not found pages you have to setup customErrors in web.config
<system.web>
<customErrors mode="On" defaultRedirect="~/error">
<error statusCode="404" redirect="~/error/notfound"></error>
</customErrors>
When 404 error is thrown, custom page still needs a route to show the error page. To solve that problem just add routes for all of the customErrors.
routes.MapRoute(
"404-PageNotFound",
"error/notfound",
new { controller = "ErrorController", action = "PageNotFound" }
);
You could setup customErrors to point to a single page which will handle all the errors, but it is not the best solution.
精彩评论