Example #1
0
        /// <inheritdoc/>
        public override bool TryMatch(ODataPathSegment pathSegment, IDictionary <string, object> values)
        {
            OperationImportSegment other = pathSegment as OperationImportSegment;

            if (other == null)
            {
                return(false);
            }

            IEdmOperationImport operationImport = Segment.OperationImports.First();
            IEdmOperationImport otherImport     = other.OperationImports.First();

            // for unbound action, just compare the action import
            if (operationImport.IsActionImport() && otherImport.IsActionImport())
            {
                return(operationImport == otherImport);
            }
            else if (operationImport.IsFunctionImport() && otherImport.IsFunctionImport())
            {
                // but for unbound function, we should compare the parameter names and
                // process the parameter values into odata routes.
                if (operationImport.Name != otherImport.Name)
                {
                    return(false);
                }

                IDictionary <string, object> parameterValues = new Dictionary <string, object>();
                foreach (var parameter in other.Parameters)
                {
                    object value = other.GetParameterValue(parameter.Name);
                    parameterValues[parameter.Name] = value;
                }

                if (RoutingConventionHelpers.TryMatch(ParameterMappings, parameterValues, values))
                {
                    foreach (var operationSegmentParameter in other.Parameters)
                    {
                        string name  = operationSegmentParameter.Name;
                        object value = parameterValues[name];

                        RoutingConventionHelpers.AddFunctionParameters((IEdmFunction)otherImport.Operation, name,
                                                                       value, values, values, ParameterMappings);
                    }

                    return(true);
                }
            }

            return(false);
        }
Example #2
0
        public static void AddFunctionParameterToRouteData(this IWebApiControllerContext controllerContext, OperationImportSegment importSegment)
        {
            Contract.Assert(controllerContext != null);
            Contract.Assert(importSegment != null);

            IDictionary <string, object> routingConventionsStore = controllerContext.Request.Context.RoutingConventionsStore;

            IEdmFunctionImport functionImport = importSegment.OperationImports.First() as IEdmFunctionImport;

            if (functionImport == null)
            {
                return;
            }

            IEdmFunction function = functionImport.Function;

            foreach (OperationSegmentParameter parameter in importSegment.Parameters)
            {
                string name  = parameter.Name;
                object value = importSegment.GetParameterValue(name);

                AddFunctionParameters(function, name, value, controllerContext.RouteData,
                                      routingConventionsStore, null);
            }

            // Append the optional parameters into RouteData.
            ODataOptionalParameter optional = new ODataOptionalParameter();

            foreach (var optionalParameter in function.Parameters.OfType <IEdmOptionalParameter>())
            {
                if (!importSegment.Parameters.Any(c => c.Name == optionalParameter.Name))
                {
                    optional.Add(optionalParameter);
                }
            }

            if (optional.OptionalParameters.Any())
            {
                controllerContext.RouteData.Add(ODataRouteConstants.OptionalParameters, optional);
            }
        }