I have an MVC 2 application that should always give a 'nice' 404 page.
However currently I get a low level .Net one: "Server Error in '/sitename' Application..."
I have a base controller that has a NotFound
action that will render the nice 404 page.
Missing actions are handled:
protected override void HandleUnknownAction(string actionName)
{
this.NotFound(actionName).ExecuteResult(this.ControllerContext);
}
So a visit to {site}/ValidController/NotAnAction
gets routed correctly.
However a visit to {site}/NotAController
doesn't.
I have routes set up with a catch all:
routes.MapRoute(
"MVC routes",
"{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional });
routes.MapRoute(
"Catch All",
"{*url}",
new { controller = "System", action = "NotFound" });
The catch all correctly catches routes that don't match.
So {site}/Invalid/Action/id/extra
is correctly routed via the catch all.
However {site}/Invalid
gets picked up via the "MVC routes" route and ASP.Net goes looking for InvalidController
, and throws a dumb exception when it doesn't find it.
I know that I can override this at the web.config
level, but that just redirects to the page. I'd like to know when the route pattern matches but the controller is not a valid controller name.
Where can I catch and change this behaviour?