Home RouteAttribute's Order is completely ignored on IRouteConstraint
Reply: 0

RouteAttribute's Order is completely ignored on IRouteConstraint

user1576 Published in August 15, 2018, 11:16 am

We have an IRouteConstraint that is getting checked much more than it should. Upon further testing, it looks like that Order on [Route] gets ignored by route constraints.

For example, if I have the following constraint:

public class TestConstraint : IRouteConstraint {
    public bool Match(
        HttpContextBase httpContext,
        Route route,
        string parameterName,
        RouteValueDictionary values,
        RouteDirection routeDirection
    ) {
        return true;

And wire it up:

constraintResolver.ConstraintMap.Add("testConstraint", typeof(TestConstraint));

And have the following routes:

public partial class HomeController {
    [Route("test/0", Order = 1)]
    public ActionResult Test0() {
        return Content("Test0");

    [Route("{someParam}/{test:testConstraint}", Order = 10)]
    public ActionResult Test1() {
        return Content("Test1");

And then make a request for http://localhost/test/0, it will return the proper content (Test0), but TestContraint.Match() is still executed.

I would think that route constraints are only executed once the route is encountered in the RouteTable, but it seems to run it on every request that can match the [Route] pattern.

If it makes a difference, we are on ASP.NET MVC v5.2.4.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.29385 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO