---
title: "How to handle 404 in ASP.NET MVC?"  
description: "How to handle 404 in ASP.NET MVC?"  
author: "Royce Roy"  
published: 2015-02-02  
updated: 2015-02-02  
canonical: https://www.mindstick.com/forum/12930/how-to-handle-404-in-asp-dot-net-mvc  
category: "asp.net"  
tags: ["c#", "asp.net mvc", "mvc4"]  
reading_time: 2 minutes  

---

# How to handle 404 in ASP.NET MVC?

I am just getting started on [ASP.NET MVC](https://www.mindstick.com/forum/155798/what-is-caching-in-asp-dot-net-mvc) so bear with me. I've searched around this site and various others and have seen a few implementations of this.

EDIT: I forgot to mention I am using RC2

**Using [URL Routing](https://www.mindstick.com/articles/1573/url-routing-in-mvc-4):**

routes.MapRoute(

"Error",

"{*url}",

new { [controller](https://www.mindstick.com/blog/273/passing-values-from-controller-to-view-in-asp-dot-net-mvc) = "Errors", action = "NotFound" } // 404s

);

The above seems to take care of requests like this (assuming default route tables setup by initial [MVC project](https://www.mindstick.com/forum/158706/how-do-you-establish-a-database-connection-in-an-asp-dot-net-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](https://yourviews.mindstick.com/audio/1151/effective-preparation-strategies-for-jee-exams-a-comprehensive-guide-for-students) 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](https://www.mindstick.com/articles/23199/digital-personal-server-the-premium-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](https://www.mindstick.com/forum/1654/can-anybody-say-my-below-code-is-a-variable-function-or-something-else) in the MVC framework itself?*

OR should I just default to using [Web.Config](https://www.mindstick.com/forum/183/web-config-file) 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](https://www.mindstick.com/news/2343/a-handbook-for-social-media-parental-restrictions) on direct access. Anyway any [best practices](https://www.mindstick.com/articles/337564/building-a-microservices-architecture-with-laravel-best-practices) or guidance is appreciated.

## Replies

### Reply by Anonymous User

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));}
```


---

Original Source: https://www.mindstick.com/forum/12930/how-to-handle-404-in-asp-dot-net-mvc

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
