예제 #1
0
        public RouteResolutionResult Apply(object request, string httpMethod)
        {
            if (!this.IsValid)
            {
                return(RouteResolutionResult.Error(this, this.ErrorMsg));
            }

            if (HttpMethods != null && HttpMethods.Length != 0 && httpMethod != null && !HttpMethods.Contains(httpMethod) && !HttpMethods.Contains("ANY"))
            {
                return(RouteResolutionResult.Error(this, "Allowed HTTP methods '{0}' does not support the specified '{1}' method."
                                                   .Fmt(HttpMethods.Join(", "), httpMethod)));
            }

            var uri = this.Path;

            var unmatchedVariables = new List <string>();

            foreach (var variable in this.variablesMap)
            {
                var property   = variable.Value;
                var value      = property.GetValue(request);
                var isWildCard = variable.Key.EndsWith("*");
                if (value == null && !isWildCard)
                {
                    unmatchedVariables.Add(variable.Key);
                    continue;
                }

                var variableValue = FormatVariable(value);
                uri = uri.Replace(VariablePrefix + variable.Key + VariablePostfix, variableValue);
            }

            if (unmatchedVariables.Any())
            {
                var errMsg = "Could not match following variables: " + string.Join(",", unmatchedVariables.ToArray());
                return(RouteResolutionResult.Error(this, errMsg));
            }

            return(RouteResolutionResult.Success(this, uri));
        }