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. namespace IngoreRoute.Controllers
  7. {
  8. public class StudentController: Controller
  9. {
  10. // GET: Student
  11. public ActionResult Index()
  12. {
  13. return View();
  14. }
  15. }
  16. }
view

Add View For Index Action

view

view

Index View Code
  1. @ {
  2. ViewBag.Title = "Index";
  3. }
  4. < 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. namespace IngoreRoute
  8. {
  9. public class RouteConfig
  10. {
  11. public static void RegisterRoutes(RouteCollection routes)
  12. {
  13. routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  14. routes.IgnoreRoute("Employee/");
  15. routes.MapRoute(
  16. name: "Default",
  17. url: "{controller}/{action}/{id}",
  18. defaults: new
  19. {
  20. controller = "Student", action = "Index", id = UrlParameter.Optional
  21. }
  22. );
  23. }
  24. }
  25. }
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.