Ignore Route In MVC

Ignore Route in MVC

While working on one of my recent projects, we had several controllers and in one of the controllers we found a big issue. We needed to solve the issue in a production environment; until then, we needed to ignore the request and response process until the issue was fixed.

Scenario:

We have two controllers in our project, Student and Employee, and we have some issue with the Employee controller. Until the issue is solved, we need to stop the user accessing the Employee controller using the RegisterRoutes method in Routeconfg.cs, 

By adding

routes.IgnoreRoute("Employee/");


Create an MVC Project

Select Empty Template and Add MVC folder reference,

Template 

Add New Controller in Controller folder,

Controller

Add Student Controller,

Student 

Student Controller

s1

Controller Code

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6.   
  7. namespace IngoreRoute.Controllers   
  8. {  
  9.     public class StudentController: Controller   
  10.     {  
  11.         // GET: Student  
  12.         public ActionResult Index()   
  13.         {  
  14.             return View();  
  15.         }  
  16.     }  
  17. }  
view

Add View For Index Action

view

view

Index View Code
  1. @ {  
  2.     ViewBag.Title = "Index";  
  3. }  
  4.   
  5. < h2 > Student Controller - Index Action Method Get Invoked < /h2>  
Index

Employee Controller

Followthe above process for how we added the Student Controller, just as we did the Employee Controller.

Run the Application

Before running the Application we need to set a startup controller as Student Controller and Index action in the Routeconfg.cs file.
  1. routes.MapRoute  
  2. (  
  3.     name: "Default",  
  4.     url: "{controller}/{action}/{id}",  
  5.     defaults: new   
  6.     {  
  7.         controller = "Student", action = "Index", id = UrlParameter.Optional  
  8.     }  
  9. );  
route

Run the Application

app

Change the Url to Employee Controller.

emp

In above image, the Employee controller is invoked successfully, but our scenario does not want to access Employee controller. In this situation we need to change the routeconfig.cs file.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using System.Web.Routing;  
  7.   
  8. namespace IngoreRoute  
  9. {  
  10.     public class RouteConfig  
  11.     {  
  12.         public static void RegisterRoutes(RouteCollection routes)  
  13.       {  
  14.             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
  15.   
  16.             routes.IgnoreRoute("Employee/");  
  17.   
  18.             routes.MapRoute(  
  19.                 name: "Default",  
  20.                 url: "{controller}/{action}/{id}",  
  21.                 defaults: new   
  22.                 {  
  23.                     controller = "Student", action = "Index", id = UrlParameter.Optional  
  24.                 }  
  25.             );  
  26.         }  
  27.     }  
  28. }  
Add routes.IgnoreRoute("Employee/");

IgnoreRoute

Run the Application and try to navigate to Employee Controller. Here's the output,

Result

When we try to use Employee Controller, we get 404 error.

Thanks for reading the article.

 

Up Next
    Ebook Download
    View all
    Learn
    View all