BuddyJoe
BuddyJoe

Reputation: 71111

ASP.NET MVC Controllers and Extending the Framework

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:

http://localhost:54321/foobar

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

Answers (2)

Robert Harvey
Robert Harvey

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

Jon Galloway
Jon Galloway

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

Related Questions