2

I am trying to create a route constraint but not sure what would be the best for this. Here is the route with no constraint :

context.MapRoute(
    "Accommodation_accomm_tags",
    "accomm/{controller}/{action}/{tag}",
    new { action = "Tags", controller = "AccommProperty" },
    new { tag = @"" } //Here I would like to put a RegEx for not null match
);

What would be the best solution for this?

3 Answers 3

8

Could you create an IRouteConstraint:

public class NotNullRouteConstraint : IRouteConstraint
{
  public bool Match(
    HttpContextBase httpContext, Route route, string parameterName, 
    RouteValueDictionary values, RouteDirection routeDirection)
  {
    return (values[parameterName] != null);
  }
}

Which you can wire up:

context.MapRoute(
  "Accommodation_accomm_tags",
  "accomm/{controller}/{action}/{tag}",
  new { action = "Tags", controller = "AccommProperty" },
  new { tag = new NotNullRouteConstraint() }
);
Sign up to request clarification or add additional context in comments.

1 Comment

I thought that too and this will be the solution if I cannot find a RegEx for not null. Thanks !
6

Why do you need a constraint for a not null/empty match? Normally if you define your route like this:

context.MapRoute(
    "Accommodation_accomm_tags",
    "accomm/{controller}/{action}/{tag}",
    new { action = "Tags", controller = "AccommProperty" },
);

and tag is not specified in the request url this route simply won't match.

And if you want a token to be optional, then:

context.MapRoute(
    "Accommodation_accomm_tags",
    "accomm/{controller}/{action}/{tag}",
    new { action = "Tags", controller = "AccommProperty", tag = UrlParameter.Optional },
);

Constraints are used when you want to constrain the value of a given route token to some specific format.

2 Comments

You nailed it again. I was trying to remember that there should be a some easy way for that but I couldn't. now it is clear. Thanks!
What if your action waiting for string parameter? In this case, if you'll specify tag as optional parameter it will contain null value.
2

At first, I tried to create a RegEx for empty string which is ^$ (which is what null would be). However, it doesn't look like route constraints can be !=. How about matching one or more character with ^.+$?

So:

tag = @"^.+$"

5 Comments

you have two sentences which are totally opposite to each other : I believe you want a RegEx for empty string... and ...you want not empty string of course...
Well he asks for a RegExp for null which is actually empty string but he wants the opposite of that for the route constraint.
I clearly pointed out that I need a RegEx for not null string. you can see it as comment inside my code sample : //Here I would like to put a RegEx for not null match I didn't ask for null match RegEx anywhere. Sorry but appreciate your effort though.
@bzlm it wasn't there. see the update history : stackoverflow.com/posts/7501141/revisions
@Cymen he he :) thanks, gonna give it a try. (don't hit the coffee hard though, not so healthy. you know what I mean :))

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.