Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

404 Http error handler in Asp.Net MVC (RC 5)

How can I Handler 404 errors without the framework throwing an Exception 500 error code?

like image 775
MrByte Avatar asked Sep 20 '08 17:09

MrByte


3 Answers

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();
}
like image 95
dave Avatar answered Nov 05 '22 05:11

dave


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.

like image 35
Haacked Avatar answered Nov 05 '22 06:11

Haacked


throw new HttpException(404, "Resource Not Found");

like image 4
Adam Avatar answered Nov 05 '22 05:11

Adam