Handling 404 Not Found globally in MVC applications

The Kentico.Web.Mvc integration package comes with 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)

Prerequisites:

For this feature to work, the <modules> element in the <system.webServer> section of your MVC project’s main web.config file needs to have the runAllManagedModulesForAllRequests attribute set to true. The attribute is automatically added by installing the Kentico.Web.Mvc integration package.




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


Enabling the global Not Found handler in MVC applications

  1. Install the Kentico.Web.Mvc integration package in your MVC application project.

  2. In the MVC application’s Global.asax file, add the following line in the Application_Start method:

    
    
    
     protected void Application_Start()
     {
         ...
    
         // Enable and configure the selected Kentico ASP.NET MVC integration features
         ApplicationConfig.RegisterFeatures(ApplicationBuilder.Current);
     }
    
    
     
  3. Make sure the feature is enabled in the MVC application’s ApplicationConfig.csfile:

    
    
    
     public static void RegisterFeatures(ApplicationBuilder builder)
     {
         ...
         builder.UseNotFoundHandler();
         ...
     }
    
    
     
  4. In your MVC application’s project, create a ‘NotFound.cshtml’ view under the ‘Views\Shared’ path.

  5. Implement the view.

Now, whenever a request for a page that doesn’t 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 doesn’t 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:

    
    
    
     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.

    
    
    
     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.