-
Notifications
You must be signed in to change notification settings - Fork 122
{id:int?} should act as optional #123
Comments
What's the behavior in MVC5? I don't think any of our constraints know about optionality. |
Interesting. I was thinking optionallity could trump constraint for the same key. |
In MVC5 (MVC and Web API attribute routing) the integer constraint only gets evaluated when there is a value in this case, so I think this is a bug... |
If this were true, then global constraints via attribute routing wouldn't work at all. (there are constraints for which there is never a route value). If I try the following definition in MVC, I can get to my action with
|
Good point!, Ryan...will investigate to see why it works in case of attribute routing... |
So it seems like in case of attribute routing in MVC5, for the above scenario, the inline route template parser wraps the IntRouteConstraint inside an OptionalRouteConstraint... InlineRouteRemplateParser if (parameterConstraints.Count > 0)
{
var constraint = parameterConstraints.Count == 1 ?
parameterConstraints[0] :
new CompoundRouteConstraint(parameterConstraints);
if (isOptional)
{
// Constraints should match RouteParameter.Optional if the parameter is optional
// This prevents contraining when there's no value specified
constraint = new OptionalRouteConstraint(constraint);
}
return constraint;
} OptionalRouteConstraint |
That's pretty cool, I didn't know we dealt with that. If we want to fix this we should do it for conventional and attribute routes. |
@rynowak you are on it, or pick a victim. There is one wierdness around the Required constraint. But you got yourself in it if you wrote it, and it's not that interesting anyways. |
Added OptionalRouteConstraint class to take care of optional inline parameter. It will create the OptionalRouteConstraint for a inline parameter that is optional with real constraint on the parameter as inner constraint of OptionalRouteConstraint.
f549a55 -- this fixes the routing part. |
No description provided.
The text was updated successfully, but these errors were encountered: