How can I Handler 404 erros without framewok throw Execption 500 error code
+12
A:
http://jason.whitehorn.ws/2008/06/17/Friendly-404-Errors-In-ASPNET-MVC.aspx gives the following explanation:
Add a wildcard routing rule as your final rule:
routes.MapRoute("Error",
"{*url}",
new { controller = "Error", action = "Http404" });
Any request that doesn't match another rule gets routed to the Http404 action of the Error controller, which you also need to configure:
public ActionResult Http404(string url) {
Response.StatusCode = 404;
ViewData["url"] = url;
return View();
}
dave
2008-09-20 17:38:37
Just an FYI, The above linked post is returning a 404 (oh the irony). The new address is: http://jason.whitehorn.ws/2008/06/17/Friendly-404-Errors-In-ASPNET-MVC.aspx
Jason Whitehorn
2008-11-25 04:21:55
The only problem here is that so much matches the typical /{controller}/{action}/{id} route. To get around the problem, I explicitly defined all my routes and got rid of it.
BC
2009-03-15 01:31:55
Unfortunatelly the link doesn't work. Even http://jason.whitehorn.ws/ is not accessible :|
stej
2009-08-12 06:35:27
+8
A:
You can also override HandleUnknownAction within your controller in the cases where a request does match a controller, but doesn't match an action. The default implementation does raise a 404 error.
Haacked
2008-09-21 17:48:17
Good idea. Check out this solution which incorporates a `HandleUnknownAction` override: http://stackoverflow.com/questions/619895/how-can-i-properly-handle-404s-in-asp-net-mvc/2577095#2577095
cottsak
2010-04-05 05:57:31