Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between RouteCollection.Ignore and RouteCollection.IgnoreRoute?

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.

like image 357
patridge Avatar asked Jul 18 '12 14:07

patridge


People also ask

What does the Ignoreroute () method do?

Ignores the specified URL route for the given list of available routes.

What is difference between conventional and attribute routing?

In short, Convention Routing approaches Routing from the general case; you generally add some routes that will match all or most of your URLs, then add more specific routes for more specialized cases. The other way to approach this problem is via Attribute Routing.

What is the difference between MVC routing and Web API routing?

If you are familiar with ASP.NET MVC, Web API routing is very similar to MVC routing. The main difference is that Web API uses the HTTP verb, not the URI path, to select the action. You can also use MVC-style routing in Web API.


1 Answers

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;
    }
}
like image 130
patridge Avatar answered Oct 13 '22 04:10

patridge