What's the difference between RouteCollection.Ignore(url, constraints)
and RouteCollection.IgnoreRoute(url, constraints)
?
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.
Ignores the specified URL route for the given list of available routes.
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.
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.
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);
}
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With