In my last article I discussed how to use a wild card route to capture completely malformed URLs. But what if a URL matches to a controller in which the requested action cannot be found?
For example, when a visitor requests http://example.com/product/listing when there is no listing action on the product controller. In this example your visitor would be greeted by the a less than friendly page stating:
Server Error in '/' Application. The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
This page is meaningless to our visitors, and unlike last time we have a general idea of what our visitor is looking for. In our example the user is interested in something to do with products. In this scenario we can override the base controller’s HandleUnknownAction method.
The HandleUnknownAction method will get fired whenever a controller cannot find a requested action.
Since this is implemented on a per controller basis we already know a lot about the context of the request and can generate a more intelligent 404 page for our visitors. For a products controller you could list popular products. For other controllers you may even use the actionName parameter of the HandleUnknownAction method to add even more intelligence to your 404 page.
The only thing to keep in mind is that no matter the content displayed these are 404 error pages, and should be treated as such. Tell your visitors that what they’ve requested cannot be found. And tell their web browsers the same by setting the Http Status code to 404.