Difference between RouteCollection.Ignore and RouteCollection.IgnoreRoute?
Asked Answered
B

1

18

What's the difference between RouteCollection.Ignore(url, constraints) and RouteCollection.IgnoreRoute(url, constraints)?

Background

New MVC projects include this IgnoreRoute call in Global.asax RegisterRoutes method to skip routing for requests to .axd locations that are handled elsewhere in the ASP.NET system.

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

I wanted to add an additional ignored route to a project and I started to type out the new line. After routes.I, Intellisense pops up with .Ignore and .IgnoreRoute, both sounding about the same.

According to the MSDN docs, you can see that one is an instance method of the System.Web.Routing.RouteCollection class and the other is an extension method on that class from System.Web.Mvc.RouteCollectionExtensions.

  • RouteCollection.Ignore: "Defines a URL pattern that should not be checked for matches against routes if a request URL meets the specified constraints" (MSDN docs).
  • RouteCollection.IgnoreRoute: "Ignores the specified URL route for the given list of the available routes and a list of constraints" (MSDN docs).

Both take a route URL pattern and a set of constraints restricting the application of the route on that URL pattern.

Breeding answered 18/7, 2012 at 14:58 Comment(0)
B
20

Between the source for System.Web.Mvc.RouteCollectionExtensions on CodePlex and running a little ILSpy on my local GAC for System.Web.Routing.RouteCollection, it doesn't appear there is a difference, though they seem to have completely independent code to do the same thing.

RouteCollection.IgnoreRoute (via CodePlex source)

public static void IgnoreRoute(this RouteCollection routes, string url, object constraints) {
    if (routes == null) {
        throw new ArgumentNullException("routes");
    }
    if (url == null) {
        throw new ArgumentNullException("url");
    }

    IgnoreRouteInternal route = new IgnoreRouteInternal(url) {
        Constraints = new RouteValueDictionary(constraints)
    };

    routes.Add(route);
}

RouteCollection.Ignore (via ILSpy decompile)

public void Ignore(string url, object constraints) {
    if (url == null) {
        throw new ArgumentNullException("url");
    }
    RouteCollection.IgnoreRouteInternal item = new RouteCollection.IgnoreRouteInternal(url) {
        Constraints = new RouteValueDictionary(constraints)
    };
    base.Add(item);
}

Differences

The only real difference is the obvious difference in location, one being an instance method in the RouteCollection class itself and one being an extensions method on that class. After you factor in the code differences that come from instance vs. extension execution (like the vital null check on the extended instance), they appear identical.

At their core, they both use the exact same StopRoutingHandler class. Both have their own versions of a sealed IgnoreRouteInternal class, but those versions are identical in code.

private sealed class IgnoreRouteInternal : Route {
    public IgnoreRouteInternal(string url)
        : base(url, new StopRoutingHandler()) {
    }
    public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary routeValues) {
        return null;
    }
}
Breeding answered 18/7, 2012 at 14:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.