Reputation: 71111
Is there any way to catch a "controller not found" event from the framework? Maybe this event doesn't exist. But if I just pick a URL to throw at the routing system of the framework like:
The final result without modification is a 404 error.
Is the easiest way to catch this through a custom ControllerFactory? Then handle it from there.
UPDATE: I'm looking for a way to catch and handle earlier in the request flow. Not at the controller level. See comments to Jon Galloway's answer.
Upvotes: 1
Views: 129
Reputation: 180808
I think there are more issues here than meets the eye.
In particular, what happens when a user invokes a valid action on a valid controller, and the requested resource — product, article, sub-category etc — doesn’t exist?
Also, for search engine purposes, the invalid URL needs to return a true 404 error.
See the following post for a detailed examination of these issues:
Strategies for resource-based 404 errors in ASP.NET MVC
http://richarddingwall.name/2008/08/17/strategies-for-resource-based-404-errors-in-aspnet-mvc/
Upvotes: 1
Reputation: 53125
I'd add a catchall route at the end of your route declarations and handle it there.
/* all your other routes here */
routes.MapRoute("NotHandled",
"{*url}",
new { controller = "NotFound", action = "Index" });
Upvotes: 1