---
title: Handling 404 Not Found globally in MVC applications
related:
  - https://docs.kentico.com/k10/developing-websites/developing-sites-using-asp-net-mvc/starting-with-mvc-development.md
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

The **Kentico.Web.Mvc** [integration package](https://docs.kentico.com/k10/developing-websites/developing-sites-using-asp-net-mvc/starting-with-mvc-development/installing-kentico-integration-packages.md) includes a feature that allows you to return the same '404 Not Found' page in the MVC application in the following scenarios:

- A suitable controller is not found
- A suitable controller action is not found
- A controller action returns _System.Web.Mvc.HttpNotFoundResult_ (usually returned by calling the controller's _HttpNotFound_ method)

## Prerequisite

The _\<modules>_ element in the _\<system.webServer>_ section of your MVC project's main _web.config_ file must have the **runAllManagedModulesForAllRequests** attribute set to _true_. The attribute is automatically added by installing the Kentico.Web.Mvc [integration package](https://docs.kentico.com/k10/developing-websites/developing-sites-using-asp-net-mvc/starting-with-mvc-development/installing-kentico-integration-packages.md).

```xml

<system.webServer>
...
    <modules runAllManagedModulesForAllRequests="true"></modules> 
...
</system.webSever> 

```

## Enabling the global Not Found handler in MVC applications

1. If necessary, set up your Kentico instance and MVC application (see [Starting with MVC development](https://docs.kentico.com/k10/developing-websites/developing-sites-using-asp-net-mvc/starting-with-mvc-development.md)).
2.  Verify that the Not Found handler feature is enabled in the MVC project's **ApplicationConfig.cs** file:

   ```csharp

   using Kentico.Web.Mvc;

   ...

   public static void RegisterFeatures(ApplicationBuilder builder)
   {
       ...
       builder.UseNotFoundHandler();
       ...
   }

   ```
3. Create a **NotFound.cshtml** view under the _Views\Shared_ path in your MVC project.
4. Implement the content of the view to design your site's Not found page.

Now whenever a request for a page that does not match any controller or controller action is made, the system returns the _NotFound.cshtml_ page. The system also returns the page whenever a controller action returns the \*System.Web.Mvc.\*_HttpNotFoundResult_ class (for example, when calling the _System.Web.Mvc.Controller.HttpNotFound_ method).

## Handling requests not matched by any route

You can also return the  _NotFound.cshtml_ view in other cases in which the MVC application responds with a 'Not Found' status. To cover a scenario when a request that does not match any configured route is made, you need to implement a catch-all route in the RouteConfig.cs file.

1. Create a new catch-all route in the MVC application's **RouteConfig.cs** file. Make sure the route is the last route that you register in the file:

   ```csharp

   public static void RegisterRoutes(RouteCollection routes)
   {
       ...
       routes.MapRoute(
           name: "NotFound",
           url: "{*url}",
           defaults: new { controller = "HttpErrors", action = "NotFound" }
       ); 
   }

   ```
2. Create a controller and action for the catch-all route.

   ```csharp

   public class HttpErrorsController : Controller
   {
       public ActionResult NotFound()
       {
           Response.StatusCode = 404;
           Response.TrySkipIisCustomErrors = true;

           return View();
       }
   }

   ```

The system returns the _NotFound.cshtml_ page whenever a request for an undefined route is made on the MVC site.
