Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I properly handle 404 in ASP.NET MVC?

I am using RC2

Using URL Routing:

routes.MapRoute(     "Error",      "{*url}",      new { controller = "Errors", action = "NotFound" }  // 404s ); 

The above seems to take care of requests like this (assuming default route tables setup by initial MVC project): "/blah/blah/blah/blah"

Overriding HandleUnknownAction() in the controller itself:

// 404s - handle here (bad action requested protected override void HandleUnknownAction(string actionName) {     ViewData["actionName"] = actionName;     View("NotFound").ExecuteResult(this.ControllerContext); }   

However the previous strategies do not handle a request to a Bad/Unknown controller. For example, I do not have a "/IDoNotExist", if I request this I get the generic 404 page from the web server and not my 404 if I use routing + override.

So finally, my question is: Is there any way to catch this type of request using a route or something else in the MVC framework itself?

OR should I just default to using Web.Config customErrors as my 404 handler and forget all this? I assume if I go with customErrors I'll have to store the generic 404 page outside of /Views due to the Web.Config restrictions on direct access.

like image 249
Brian Avatar asked Mar 06 '09 18:03

Brian


People also ask

How does MVC handle wrong URL?

First: Add a route pattern to the end of your routing table that matches to any URL. Second: Set the default controller and action values for this pattern to the controller/action method that will display the "more helpful" View you want to provide. (And, I guess, a third step: Provide that controller/action/View.)

How do I manage my 404 page?

Use Redirects When Possible Often, it's best to not show a 404 page at all, and instead simply redirect people to a different page. If you update a page and change the URL, make sure you redirect the old URL to the new page so visitors don't wind up on a 404 page.

What do you mean by 404 in asp net?

Best web development and SEO practices dictate that any webpage which does not exist, return an HTTP response code of 404, or Not Found. Basically, this response code means that the URL that you're requesting does not exist.


1 Answers

The code is taken from http://blogs.microsoft.co.il/blogs/shay/archive/2009/03/06/real-world-error-hadnling-in-asp-net-mvc-rc2.aspx and works in ASP.net MVC 1.0 as well

Here's how I handle http exceptions:

protected void Application_Error(object sender, EventArgs e) {    Exception exception = Server.GetLastError();    // Log the exception.     ILogger logger = Container.Resolve<ILogger>();    logger.Error(exception);     Response.Clear();     HttpException httpException = exception as HttpException;     RouteData routeData = new RouteData();    routeData.Values.Add("controller", "Error");     if (httpException == null)    {        routeData.Values.Add("action", "Index");    }    else //It's an Http Exception, Let's handle it.    {        switch (httpException.GetHttpCode())        {           case 404:               // Page not found.               routeData.Values.Add("action", "HttpError404");               break;           case 500:               // Server error.               routeData.Values.Add("action", "HttpError500");               break;             // Here you can handle Views to other error codes.            // I choose a General error template              default:               routeData.Values.Add("action", "General");               break;       }   }               // Pass exception details to the target error View.   routeData.Values.Add("error", exception);    // Clear the error on server.   Server.ClearError();    // Avoid IIS7 getting in the middle   Response.TrySkipIisCustomErrors = true;     // Call target Controller and pass the routeData.   IController errorController = new ErrorController();   errorController.Execute(new RequestContext(            new HttpContextWrapper(Context), routeData)); } 
like image 97
Shay Jacoby Avatar answered Oct 18 '22 14:10

Shay Jacoby