/// <summary> /// Initializes a new instance of the <see cref="OptionalHttpRouteConstraint" /> class. /// </summary> /// <param name="innerConstraint">The inner constraint to match if the parameter is not an optional parameter without a value</param> public OptionalHttpRouteConstraint(IHttpRouteConstraint innerConstraint) { if (innerConstraint == null) { throw Error.ArgumentNull("innerConstraint"); } InnerConstraint = innerConstraint; }
protected bool TestValue(IHttpRouteConstraint constraint, object value) { HttpRequestMessage httpRequestMessage = new HttpRequestMessage(); HttpRoute httpRoute = new HttpRoute(); const string parameterName = "fake"; HttpRouteValueDictionary values = new HttpRouteValueDictionary { { parameterName, value } }; const HttpRouteDirection httpRouteDirection = HttpRouteDirection.UriResolution; return constraint.Match(httpRequestMessage, httpRoute, parameterName, values, httpRouteDirection); }
public void Match_SucceedsWhenInnerConstraintsMatch() { IHttpRouteConstraint[] innerConstraints = new IHttpRouteConstraint[] { new AlphaHttpRouteConstraint(), new LengthHttpRouteConstraint(3) }; CompoundHttpRouteConstraint constraint = new CompoundHttpRouteConstraint(innerConstraints); bool match = TestValue(constraint, "abc"); Assert.True(match); }
public void Match_FailsWhenAnyInnerConstraintFails() { IHttpRouteConstraint[] innerConstraints = new IHttpRouteConstraint[] { new AlphaHttpRouteConstraint(), new LengthHttpRouteConstraint(3) }; CompoundHttpRouteConstraint constraint = new CompoundHttpRouteConstraint(innerConstraints); bool match = TestValue(constraint, "abcd"); Assert.False(match); }
protected override bool ProcessConstraint(HttpContextBase httpContext, object constraint, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) { // The base class will validate that a constraint is either a string or IRoutingConstraint inside its // ProcessConstraint method. We're doing the validation up front here because we also support // IHttpRouteConstraint and we want the error message to reflect all three valid possibilities. ValidateConstraint(HttpRoute.RouteTemplate, parameterName, constraint); IHttpRouteConstraint httpRouteConstraint = constraint as IHttpRouteConstraint; if (httpRouteConstraint != null) { HttpRequestMessage request = httpContext.GetOrCreateHttpRequestMessage(); return(httpRouteConstraint.Match(request, HttpRoute, parameterName, values, ConvertRouteDirection(routeDirection))); } return(base.ProcessConstraint(httpContext, constraint, parameterName, values, routeDirection)); }
private static bool TestValue(IHttpRouteConstraint constraint, object value, Action <IHttpRoute> routeConfig = null) { HttpRequestMessage httpRequestMessage = new HttpRequestMessage(); HttpRoute httpRoute = new HttpRoute(); if (routeConfig != null) { routeConfig(httpRoute); } const string parameterName = "fake"; HttpRouteValueDictionary values = new HttpRouteValueDictionary { { parameterName, value } }; const HttpRouteDirection httpRouteDirection = HttpRouteDirection.UriResolution; return(constraint.Match(httpRequestMessage, httpRoute, parameterName, values, httpRouteDirection)); }
protected override bool ProcessConstraint(HttpContextBase httpContext, object constraint, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) { IHttpRouteConstraint httpRouteConstraint = constraint as IHttpRouteConstraint; if (httpRouteConstraint != null) { HttpRequestMessage request = httpContext.GetHttpRequestMessage(); if (request == null) { request = HttpControllerHandler.ConvertRequest(httpContext); httpContext.SetHttpRequestMessage(request); } return(httpRouteConstraint.Match(request, HttpRoute, parameterName, values, ConvertRouteDirection(routeDirection))); } return(base.ProcessConstraint(httpContext, constraint, parameterName, values, routeDirection)); }
protected virtual bool ProcessConstraint( HttpRequestMessage request, object constraint, string parameterName, HttpRouteValueDictionary values, HttpRouteDirection routeDirection ) { IHttpRouteConstraint customConstraint = constraint as IHttpRouteConstraint; if (customConstraint != null) { return(customConstraint.Match(request, this, parameterName, values, routeDirection)); } // If there was no custom constraint, then treat the constraint as a string which represents a Regex. string constraintsRule = constraint as string; if (constraintsRule == null) { throw Error.InvalidOperation( SRResources.Route_ValidationMustBeStringOrCustomConstraint, parameterName, RouteTemplate, typeof(IHttpRouteConstraint).Name ); } object parameterValue; values.TryGetValue(parameterName, out parameterValue); string parameterValueString = Convert.ToString( parameterValue, CultureInfo.InvariantCulture ); string constraintsRegEx = "^(" + constraintsRule + ")$"; return(Regex.IsMatch( parameterValueString, constraintsRegEx, RegexOptions.CultureInvariant | RegexOptions.IgnoreCase )); }
/// <summary> /// Initializes a new instance of the <see cref="ODataRoute" /> class. /// </summary> /// <param name="routePrefix">The route prefix.</param> /// <param name="routeConstraint">The route constraint.</param> /// <param name="defaults">The default values for the route.</param> /// <param name="constraints">The route constraints.</param> /// <param name="dataTokens">The data tokens.</param> /// <param name="handler">The message handler for the route.</param> /// <remarks>This signature uses types that are AspNet-specific.</remarks> public ODataRoute( string routePrefix, IHttpRouteConstraint routeConstraint, HttpRouteValueDictionary defaults, HttpRouteValueDictionary constraints, HttpRouteValueDictionary dataTokens, HttpMessageHandler handler) : base(GetRouteTemplate(routePrefix), defaults, constraints, dataTokens, handler) { RouteConstraint = routeConstraint; Initialize(routePrefix, routeConstraint as ODataPathRouteConstraint); if (routeConstraint != null) { Constraints.Add(ODataRouteConstants.ConstraintName, routeConstraint); } Constraints.Add(ODataRouteConstants.VersionConstraintName, new ODataVersionConstraint()); }
public override bool ShouldExploreController(string controllerVariableValue, HttpControllerDescriptor controllerDescriptor, IHttpRoute route) { if (route == null) { throw new ArgumentNullException("route"); } bool result = base.ShouldExploreController(controllerVariableValue, controllerDescriptor, route); object constraint; if (route.Constraints.TryGetValue("controller", out constraint)) { IHttpRouteConstraint typedConstraint = constraint as IHttpRouteConstraint; return(result && typedConstraint.Match(null, route, "controller", new Dictionary <string, object> { { "controller", controllerVariableValue } }, HttpRouteDirection.UriResolution)); } return(result); }
private static void SetSwaggerType(Parameter parameter, IHttpRouteConstraint routeConstraint) { if (routeConstraint is BoolRouteConstraint) { SetSwaggerType(parameter, EdmLibHelpers.GetEdmPrimitiveTypeOrNull(typeof(bool))); } else if (routeConstraint is DateTimeRouteConstraint) { SetSwaggerType(parameter, EdmLibHelpers.GetEdmPrimitiveTypeOrNull(typeof(DateTime))); } else if (routeConstraint is DecimalRouteConstraint) { SetSwaggerType(parameter, EdmLibHelpers.GetEdmPrimitiveTypeOrNull(typeof(decimal))); } else if (routeConstraint is DoubleRouteConstraint) { SetSwaggerType(parameter, EdmLibHelpers.GetEdmPrimitiveTypeOrNull(typeof(double))); } else if (routeConstraint is FloatRouteConstraint) { SetSwaggerType(parameter, EdmLibHelpers.GetEdmPrimitiveTypeOrNull(typeof(float))); } else if (routeConstraint is GuidRouteConstraint) { SetSwaggerType(parameter, EdmLibHelpers.GetEdmPrimitiveTypeOrNull(typeof(Guid))); } else if (routeConstraint is IntRouteConstraint) { SetSwaggerType(parameter, EdmLibHelpers.GetEdmPrimitiveTypeOrNull(typeof(int))); } else if (routeConstraint is LongRouteConstraint) { SetSwaggerType(parameter, EdmLibHelpers.GetEdmPrimitiveTypeOrNull(typeof(long))); } else { SetSwaggerType(parameter, EdmLibHelpers.GetEdmPrimitiveTypeOrNull(typeof(string))); } }
private string ParseRouteTemplate(string routeTemplate, HttpRouteValueDictionary defaults, HttpRouteValueDictionary constraints) { Contract.Assert(defaults != null); Contract.Assert(constraints != null); MatchCollection parameterMatches = _parameterRegex.Matches(routeTemplate); foreach (Match parameterMatch in parameterMatches) { string parameterName = parameterMatch.Groups["parameterName"].Value; // We may need to strip out the initial wildcard used for wildcard parameters if (parameterName.StartsWith("*", StringComparison.OrdinalIgnoreCase)) { parameterName = parameterName.Substring(1); } // Add the default value if present Group defaultValueGroup = parameterMatch.Groups["defaultValue"]; object defaultValue = GetDefaultValue(defaultValueGroup); if (defaultValue != null) { defaults.Add(parameterName, defaultValue); } // Register inline constraints if present Group constraintGroup = parameterMatch.Groups["constraint"]; bool isOptional = defaultValue == RouteParameter.Optional; IHttpRouteConstraint constraint = GetInlineConstraint(constraintGroup, isOptional); if (constraint != null) { constraints.Add(parameterName, constraint); } } // Replaces parameter matches with just the parameter name in braces // Strips out the optional '?', default value, inline constraints return(_parameterRegex.Replace(routeTemplate, @"{${parameterName}}")); }
private static IHttpRouteConstraint ConstraintForTypename(string typename, bool isOptional) { /* List of all route constraints: * https://msdn.microsoft.com/en-us/library/system.web.http.routing.constraints(v=vs.118).aspx */ IHttpRouteConstraint constraint = null; switch (typename) { case "bool": case "boolean": constraint = new BoolRouteConstraint(); break; case "int": constraint = new IntRouteConstraint(); break; case "long": constraint = new LongRouteConstraint(); break; case "float": constraint = new FloatRouteConstraint(); break; case "double": constraint = new DoubleRouteConstraint(); break; case "string": constraint = new AlphaRouteConstraint(); break; } return(isOptional ? new OptionalRouteConstraint(constraint) : constraint); }
/// <summary> /// Initializes a new instance of the <see cref="ODataRoute" /> class. /// </summary> /// <param name="routePrefix">The route prefix.</param> /// <param name="routeConstraint">The route constraint.</param> /// <param name="defaults">The default values for the route.</param> /// <param name="constraints">The route constraints.</param> /// <param name="dataTokens">The data tokens.</param> /// <param name="handler">The message handler for the route.</param> public ODataRoute( string routePrefix, IHttpRouteConstraint routeConstraint, HttpRouteValueDictionary defaults, HttpRouteValueDictionary constraints, HttpRouteValueDictionary dataTokens, HttpMessageHandler handler) : base(GetRouteTemplate(routePrefix), defaults, constraints, dataTokens, handler) { RoutePrefix = routePrefix; PathRouteConstraint = routeConstraint as ODataPathRouteConstraint; RouteConstraint = routeConstraint; // We can only use our fast-path for link generation if there are no open brackets in the route prefix // that need to be replaced. If there are, fall back to the slow path. _canGenerateDirectLink = routePrefix == null || routePrefix.IndexOf('{') == -1; if (routeConstraint != null) { Constraints.Add(ODataRouteConstants.ConstraintName, routeConstraint); } Constraints.Add(ODataRouteConstants.VersionConstraintName, new ODataVersionConstraint()); }
/// <summary> /// Automatically applies the specified constaint against url parameters /// with names that match the given regular expression. /// </summary> /// <param name="keyRegex">The regex used to match url parameter names</param> /// <param name="constraint">The constraint to apply to matched parameters</param> public void AddDefaultRouteConstraint(string keyRegex, IHttpRouteConstraint constraint) { base.AddDefaultRouteConstraint(keyRegex, constraint); }
public OptionalRouteConstraint(IHttpRouteConstraint constraint) { _constraint = constraint; }
public QueryStringRouteConstraint(IHttpRouteConstraint constraint) { _constraint = constraint; }
/// <summary> /// Initializes a new instance of the <see cref="OptionalRouteConstraint" /> class. /// </summary> /// <param name="innerConstraint">The inner constraint to match if the parameter is not an optional parameter without a value</param> #if ASPNETWEBAPI public OptionalRouteConstraint(IHttpRouteConstraint innerConstraint)
/// <summary> /// Initializes a new instance of the <see cref="ODataRoute" /> class. /// </summary> /// <param name="routePrefix">The route prefix.</param> /// <param name="routeConstraint">The route constraint.</param> /// <remarks>This signature uses types that are AspNet-specific.</remarks> public ODataRoute(string routePrefix, IHttpRouteConstraint routeConstraint) : this(routePrefix, routeConstraint, defaults : null, constraints : null, dataTokens : null, handler : null) { }
public ApiRouter WithConstraint(string parameterName, IHttpRouteConstraint constraint) { _Constraints.Add(parameterName, constraint); return(this); }
/// <summary> /// Initializes a new instance of the <see cref="ODataRoute" /> class. /// </summary> /// <param name="routePrefix">The route prefix.</param> /// <param name="routeConstraint">The route constraint.</param> public ODataRoute(string routePrefix, IHttpRouteConstraint routeConstraint) : this(routePrefix, routeConstraint, defaults: null, constraints: null, dataTokens: null, handler: null) { }