예제 #1
0
        public override string SelectAction(ODataPath odataPath, HttpControllerContext controllerContext, ILookup <string, HttpActionDescriptor> actionMap)
        {
            var routeValues = controllerContext.RouteData.Values;

            var action = base.SelectAction(odataPath, controllerContext, actionMap);

            if (action != null)
            {
                if (routeValues.ContainsKey(ODataRouteConstants.Key))
                {
                    var keyRaw = routeValues[ODataRouteConstants.Key] as string;
                    if (keyRaw != null)
                    {
                        CompositeODataKeyHelper.TryEnrichRouteValues(keyRaw, routeValues);
                    }
                }
            }
            //Allows actions for an entity with composite key
            else if (odataPath.PathTemplate == "~/entityset/key/action" ||
                     odataPath.PathTemplate == "~/entityset/key/cast/action")
            {
                var keyValueSegment      = odataPath.Segments[1] as KeyValuePathSegment;
                var actionSegment        = odataPath.Segments.Last() as ActionPathSegment;
                var actionFunctionImport = actionSegment.Action;

                controllerContext.RouteData.Values[ODataRouteConstants.Key] = keyValueSegment.Value;
                CompositeODataKeyHelper.TryEnrichRouteValues(keyValueSegment.Value, routeValues);
                return(actionFunctionImport.Name);
            }

            return(action);
        }
        public string SelectAction(ODataPath odataPath, HttpControllerContext controllerContext, ILookup <string, HttpActionDescriptor> actionMap)
        {
            if (odataPath.PathTemplate != "~/entityset/key/property")
            {
                return(null);
            }

            var entitySetPathSegment      = odataPath.Segments.OfType <EntitySetPathSegment>().Single();
            var keyValuePathSegment       = odataPath.Segments.OfType <KeyValuePathSegment>().Single();
            var propertyAccessPathSegment = odataPath.Segments.OfType <PropertyAccessPathSegment>().Single();

            var actionName = string.Format(CultureInfo.InvariantCulture, "GetPropertyFrom{0}", entitySetPathSegment.EntitySetName);

            if (actionMap.Contains(actionName) && actionMap[actionName].Any(desc => MatchHttpMethod(desc, controllerContext.Request.Method)))
            {
                controllerContext.RouteData.Values.Add("propertyName", propertyAccessPathSegment.PropertyName);

                if (!CompositeODataKeyHelper.TryEnrichRouteValues(keyValuePathSegment.Value, controllerContext.RouteData.Values))
                {
                    controllerContext.RouteData.Values.Add("key", keyValuePathSegment.Value);
                }

                return(actionName);
            }

            return(null);
        }