Use of StopRoutingHandler and IgnoreRouteMethod in ASP.NET MVC

In this blog we will learn about how to ignore the route for specific request.
 
When you create new asp.net MVC project.you will find the following code in RegisterRoutes() method under the App_start folder.
  1. public class RouteConfig  
  2.    {  
  3.        public static void RegisterRoutes(RouteCollection routes)  
  4.        {  
  5.            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
  6.            routes.MapRoute(  
  7.                name: "Default",  
  8.                url: "{controller}/{action}/{id}",  
  9.                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }  
  10.            );  
  11.        }  
  12.    } 
 routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
StopRoutingHandler and IgnoreRoute method is used to exclude the URL from route. Here the url with the pattern {resource}.axd/{*pathInfo} is use to prevent requests for resource files(i.e. WebResource.axd,ScriptResource.axd).
so when the request comes for /WebResource.axd or /ScriptResource.axd resource file, it will match by the first route, since that route will ignore the request and bypass that request to normal ASP.NET processing.
 
Notes:

By default ,routing ignores the incoming request URL that directly map to physical files such as image,CSS,JavaScript files.so you don't need to add the IgnoreRoute method for these type of resources.
 
however,there may be a situation where you need to ignore the route for the request that is not directly mapped to physical file.at that time you can use StopRoutingHandler or IgnoreRoute method.