/// <summary> /// Adds a constraint for the given key, resolved by the <see cref="IInlineConstraintResolver"/>. /// </summary> /// <param name="key">The key.</param> /// <param name="constraintText">The text to be resolved by <see cref="IInlineConstraintResolver"/>.</param> /// <remarks> /// The <see cref="IInlineConstraintResolver"/> can create <see cref="IRouteConstraint"/> instances /// based on <paramref name="constraintText"/>. See <see cref="RouteOptions.ConstraintMap"/> to register /// custom constraint types. /// </remarks> public void AddResolvedConstraint(string key, string constraintText) { if (key == null) { throw new ArgumentNullException(nameof(key)); } if (constraintText == null) { throw new ArgumentNullException(nameof(constraintText)); } var constraint = _inlineConstraintResolver.ResolveConstraint(constraintText); if (constraint == null) { throw new InvalidOperationException( Resources.FormatRouteConstraintBuilder_CouldNotResolveConstraint( key, constraintText, _displayName, _inlineConstraintResolver.GetType().Name)); } else if (constraint == NullRouteConstraint.Instance) { // A null route constraint can be returned for other parameter policy types return; } Add(key, constraint); }
/// <summary> /// Adds a constraint for the given key, resolved by the <see cref="IInlineConstraintResolver"/>. /// </summary> /// <param name="key">The key.</param> /// <param name="constraintText">The text to be resolved by <see cref="IInlineConstraintResolver"/>.</param> /// <remarks> /// The <see cref="IInlineConstraintResolver"/> can create <see cref="IRouteConstraint"/> instances /// based on <paramref name="constraintText"/>. See <see cref="RouteOptions.ConstraintMap"/> to register /// custom constraint types. /// </remarks> public void AddResolvedConstraint(string key, string constraintText) { if (key == null) { throw new ArgumentNullException(nameof(key)); } if (constraintText == null) { throw new ArgumentNullException(nameof(constraintText)); } var constraint = _inlineConstraintResolver.ResolveConstraint(constraintText); if (constraint == null) { throw new InvalidOperationException( Resources.FormatRouteConstraintBuilder_CouldNotResolveConstraint( key, constraintText, _displayName, _inlineConstraintResolver.GetType().Name)); } Add(key, constraint); }
private static IRouteConstraint GetInlineConstraint(Group constraintGroup, IInlineConstraintResolver _constraintResolver) { var parameterConstraints = new List <IRouteConstraint>(); foreach (Capture constraintCapture in constraintGroup.Captures) { var inlineConstraint = constraintCapture.Value; var constraint = _constraintResolver.ResolveConstraint(inlineConstraint); if (constraint == null) { throw new InvalidOperationException( Resources.FormatInlineRouteParser_CouldNotResolveConstraint( _constraintResolver.GetType().Name, inlineConstraint)); } parameterConstraints.Add(constraint); } if (parameterConstraints.Count > 0) { var constraint = parameterConstraints.Count == 1 ? parameterConstraints[0] : new CompositeRouteConstraint(parameterConstraints); return(constraint); } return(null); }
private static IRouteConstraint GetInlineConstraint( Group constraintGroup, bool isOptional, IInlineConstraintResolver constraintResolver ) #endif { #if ASPNETWEBAPI List <IHttpRouteConstraint> parameterConstraints = new List <IHttpRouteConstraint>(); #else List <IRouteConstraint> parameterConstraints = new List <IRouteConstraint>(); #endif foreach (Capture constraintCapture in constraintGroup.Captures) { string inlineConstraint = constraintCapture.Value; var constraint = constraintResolver.ResolveConstraint(inlineConstraint); if (constraint == null) { throw Error.InvalidOperation( ErrorResources.HttpRouteBuilder_CouldNotResolveConstraint, constraintResolver.GetType().Name, inlineConstraint ); } parameterConstraints.Add(constraint); } 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); } return(null); }
/// <summary> /// Adds a constraint for the given key, resolved by the <see cref="IInlineConstraintResolver"/>. /// </summary> /// <param name="key">The key.</param> /// <param name="constraintText">The text to be resolved by <see cref="IInlineConstraintResolver"/>.</param> /// <remarks> /// The <see cref="IInlineConstraintResolver"/> can create <see cref="IRouteConstraint"/> instances /// based on <paramref name="constraintText"/>. See <see cref="RouteOptions.ConstraintMap"/> to register /// custom constraint types. /// </remarks> public void AddResolvedConstraint(string key, string constraintText) { if (key == null) { throw new ArgumentNullException(nameof(key)); } if (constraintText == null) { throw new ArgumentNullException(nameof(constraintText)); } var constraint = _inlineConstraintResolver.ResolveConstraint(constraintText); if (constraint == null) { throw new InvalidOperationException($"The constraint entry '{key}' - '{constraintText}' on the route '{_displayName}' could not be resolved by the constraint resolver of type '{_inlineConstraintResolver.GetType().Name}'."); } else if (constraint == NullRouteConstraint.Instance) { // A null route constraint can be returned for other parameter policy types return; } Add(key, constraint); }
private static IRouteConstraint GetInlineConstraint(Group constraintGroup, bool isOptional, IInlineConstraintResolver constraintResolver) #endif { #if ASPNETWEBAPI List<IHttpRouteConstraint> parameterConstraints = new List<IHttpRouteConstraint>(); #else List<IRouteConstraint> parameterConstraints = new List<IRouteConstraint>(); #endif foreach (Capture constraintCapture in constraintGroup.Captures) { string inlineConstraint = constraintCapture.Value; var constraint = constraintResolver.ResolveConstraint(inlineConstraint); if (constraint == null) { throw Error.InvalidOperation(ErrorResources.HttpRouteBuilder_CouldNotResolveConstraint, constraintResolver.GetType().Name, inlineConstraint); } parameterConstraints.Add(constraint); } 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; } return null; }