How to simulate IgnoreRoute in my regular MvcRouteHandler

In my ASP.NET MVC3 application, I am trying to simulate "routes.IgnoreRoute (" ... ") I am creating a CustomMvcRouteHandler:

public class CustomMvcRouteHandler: MvcRouteHandler { protected override IHttpHandler GetHttpHandler(RequestContext requestContext) { // do something .... return base.GetHttpHandler(requestContext); } } 

in my Global.asax.cs file I have:

 protected void Application_Start() { // ............ RegisterRoutes(RouteTable.Routes); // ............ } public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("elmah.axd"); //routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } ).RouteHandler = new CustomMvcRouteHandler(); } 

How can i do this?

+4
source share
2 answers

I'm not quite sure what you mean in your question, but I will try to answer it ...

To model IgnoreRoute all you have to do is associate the StopRoutingHandler instance with your route. If you use the built-in ASP.NET Route class, you will do something like this:

 routes.MapRoute( "Ignore-This", // Route name "ignore/{this}/{pattern}" // URL with parameters ).RouteHandler = new StopRoutingHandler(); 

Anything that matches this pattern will cause the routing system to immediately stop processing any routes.

If you want to write your own route (for example, a new type of route obtained from RouteBase ), then you need to return StopRoutingHandler from its GetRouteData method.

+1
source

@ Eilon is the correct answer. Here is an alternative syntax that looks more like MVCish.

 routes.Add("Ignore-This", new Route( "ignore/{this}/{pattern}", new StopRoutingHandler()) ); 
0
source

Source: https://habr.com/ru/post/1339682/


All Articles