Possible Duplicate:
How can I properly handle 404 in ASP.NET MVC?
I've made the changes outlined at 404 Http error handler in Asp.Net MVC (RC 5) and I'm still getting the standard 404 error page. Do I need to change something in IIS?
I've made the changes outlined at 404 Http error handler in Asp.Net MVC (RC 5) and I'm still getting the standard 404 error page. Do I need to change something in IIS? |
|||||
|
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Yet another solution. Add ErrorControllers or static page to with 404 error information. Modify you web.config (in case of controller).
Or in case of static page
This will handle both missed routes and missed actions. |
|||||||||||||||
|
I've investigated A LOT on how to properly manage 404s in MVC (specifically MVC3), and this, IMHO is the best solution I've come up with: In global.asax:
ErrorsController:
(Optional) Explanation: AFAIK, there are 6 different cases that an ASP.NET MVC3 apps can generate 404s. (Automatically generated by ASP.NET Framework:) (1) An URL does not find a match in the route table. (Automatically generated by ASP.NET MVC Framework:) (2) An URL finds a match in the route table, but specifies a non-existent controller. (3) An URL finds a match in the route table, but specifies a non-existant action. (Manually generated:) (4) An action returns an HttpNotFoundResult by using the method HttpNotFound(). (5) An action throws an HttpException with the status code 404. (6) An actions manually modifies the Response.StatusCode property to 404. Normally, you want to accomplish 3 objectives: (1) Show a custom 404 error page to the user. (2) Maintain the 404 status code on the client response (specially important for SEO). (3) Send the response directly, without involving a 302 redirection. There are various ways to try to accomplish this: (1)
Problems with this solution:
(2)
Problems with this solution:
(3)
Problems with this solution:
(4)
and
Problems with this solution:
People that have troubled with this before even tried to create their own libraries (see http://aboutcode.net/2011/02/26/handling-not-found-with-asp-net-mvc3.html). But the previous solution seems to cover all the cases without the complexity of using an external library. |
|||||||||||||||
|
Looks like this is the best way to catch everything. http://stackoverflow.com/questions/619895/how-can-i-properly-handle-404s-in-asp-net-mvc |
||||
|
The response from Marco is the BEST solution. I needed to control my error handling, and I mean really CONTROL it. Of course, I have extended the solution a little and created a full error management system that manages everything. I have also read about this solution in other blogs and it seems very acceptable by most of the advanced developers. Here is the final code that I am using:
and inside my ErrorManagerController :
Now, in my Action, I am throwing a Custom Exception that I have created. And my Controller is inheriting from a custom Controller Based class that I have created. The Custom Base Controller was created to override error handling. Here is my custom Base Controller class:
The "ErrorManager" in the above code is just a view that is using a Model based on ExceptionContext My solution works perfectly and I am able to handle ANY error on my website and display different messages based on ANY exception type. |
|||
|
What I can recomend is to look on FilterAttribute. For example MVC already has HandleErrorAttribute. You can customize it to handle only 404. Reply if you are interesed I will look example. BTW Solution(with last route) that you have accepted in previous question does not work in much of the situations. Second solution with HandleUnknownAction will work but require to make this change in each controller or to have single base controller. My choice is a solution with HandleUnknownAction. |
|||||||||
|
In IIS, you can specify a redirect to "certain" page based on error code. In you example, you can configure 404 - > Your customized 404 error page. |
|||
|