1

I am trying to enable a route like the following

route = new Route("{w1}-{c1}-{n1},{w2}-{c2}-{n2}", new ResultRouteHandler());
route.Constraints = new RouteValueDictionary();
route.Constraints.Add("c1", "(.*)|([-])");
route.Constraints.Add("c2", "(.*)|([-])");
RouteTable.Routes.Add(route);

However I run into a problem when c1 or c2 is "-". For example "a-b-c,d---f" returns 404 (whilst "a-b-c,d-e-f" works fine). Anyone has a clue what am I doing wrong? Thank you in advance.

EDIT:

I found a simple workaround for this problem:

route = new Route("{w1}-{c1}-{n1},{w2}---{n2}", new MyRouteHandler());
RouteTable.Routes.Add(route);
route = new Route("{w1}-{c1}-{n1},{w2}-{c2}-{n2}", new MyRouteHandler());
RouteTable.Routes.Add(route);

If c2 is "-" we match to the first route, otherwise to the second.

2 Answers 2

1

If I'm understanding correctly, you do want to match "a-b-c,d---f" which is why you're tweaking the constraints in the first place. The Regex you have there is kind of redundant though as '-' will be matched by the '.*'. In other words, I don't think your Regex is to blame but rather the routing engine parser.

If you change your route from "{w1}-{c1}-{n1},{w2}-{c2}-{n2}" to "{w1}-{c1}-{n1},{w2}_{c2}-{n2}", then c2 will start matching the '-' in "a-b-c,d_--f". I think something about the parsing of routes doesn't like using a delimiter as the next value.

So I think you can drop your constraints (as they currently exist), but you might need to organize your URLs a little differently if you want '-' to be c1 or c2.

Sign up to request clarification or add additional context in comments.

1 Comment

Yep it seems to be not possible to create the rule with a single route, due to the way Regex is parsed. I worked around the problem by having two routes.
0

I'm not 100% positive about the regex implementation, but it should look like this I think:

route.Constraints.Add("c1", "([^-]*)");
route.Constraints.Add("c2", "([^-]*)");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.