public string SelectAction(ODataPath odataPath, HttpControllerContext controllerContext, ILookup <string, HttpActionDescriptor> actionMap)
        {
            //var es = odataPath.Segments.FirstOrDefault(s => s.SegmentKind == "entityset") as EntitySetPathSegment;
            var nav = odataPath.Segments.FirstOrDefault(s => s.SegmentKind == "navigation") as NavigationPathSegment;

            if (odataPath.PathTemplate == "~/unboundaction")
            {
                UnboundActionPathSegment actionSegment = odataPath.Segments.First() as UnboundActionPathSegment;
                if (actionSegment != null)
                {
                    IEdmActionImport action = actionSegment.Action;

                    if (actionMap.Contains(action.Name))
                    {
                        return(action.Name);
                    }
                }
            }
            else if (odataPath.PathTemplate == "~/unboundfunction")
            {
                UnboundFunctionPathSegment functionSegment = odataPath.Segments.First() as UnboundFunctionPathSegment;
                if (functionSegment != null)
                {
                    IEdmFunctionImport function = functionSegment.Function;

                    if (actionMap.Contains(function.Name))
                    {
                        return(function.Name);
                    }
                }
            }

            if (odataPath.PathTemplate == "~/entityset/key/navigation" && nav != null)
            {
                controllerContext.RouteData.Values["key"] = (odataPath.Segments[1] as KeyValuePathSegment).Value;
                return("Get" + nav.NavigationPropertyName);
            }
            else if (odataPath.PathTemplate == "~/entityset/key/navigation/key" && nav != null)
            {
                controllerContext.RouteData.Values["key1"] = (odataPath.Segments[1] as KeyValuePathSegment).Value;
                controllerContext.RouteData.Values["key2"] = (odataPath.Segments[3] as KeyValuePathSegment).Value;
                return("Get" + nav.NavigationProperty.ToEntityType().Name);
            }

            return(null);
        }
        internal static IEdmAction GetAction(ODataDeserializerContext readContext)
        {
            if (readContext == null)
            {
                throw Error.ArgumentNull("readContext");
            }

            ODataPath path = readContext.Path;

            if (path == null || path.Segments.Count == 0)
            {
                throw new SerializationException(SRResources.ODataPathMissing);
            }

            IEdmAction action = null;

            if (path.PathTemplate == "~/unboundaction")
            {
                // only one segment, it may be an unbound action
                UnboundActionPathSegment unboundActionSegment = path.Segments.Last() as UnboundActionPathSegment;
                if (unboundActionSegment != null)
                {
                    action = unboundActionSegment.Action.Action;
                }
            }
            else
            {
                // otherwise, it may be a bound action
                BoundActionPathSegment actionSegment = path.Segments.Last() as BoundActionPathSegment;
                if (actionSegment != null)
                {
                    action = actionSegment.Action;
                }
            }

            if (action == null)
            {
                string message = Error.Format(SRResources.RequestNotActionInvocation, path.ToString());
                throw new SerializationException(message);
            }

            return(action);
        }
Beispiel #3
0
        /// <summary>
        /// Handles a POST request to an action.
        /// </summary>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The task object that contains the action result.</returns>
        public async Task <IHttpActionResult> PostAction(CancellationToken cancellationToken)
        {
            ODataPath path = this.GetPath();
            UnboundActionPathSegment actionPathSegment = path.Segments.Last() as UnboundActionPathSegment;

            if (actionPathSegment == null)
            {
                throw new NotSupportedException(Resources.PostToUnboundActionNotSupported);
            }

            ActionInvocationEntry entry = new ActionInvocationEntry(actionPathSegment.ActionName, null);

            RestierChangeSetProperty changeSetProperty = this.Request.GetChangeSet();

            if (changeSetProperty == null)
            {
                ChangeSet changeSet = new ChangeSet();
                changeSet.Entries.Add(entry);

                SubmitResult result = await Api.SubmitAsync(changeSet, cancellationToken);
            }
            else
            {
                changeSetProperty.ChangeSet.Entries.Add(entry);

                await changeSetProperty.OnChangeSetCompleted();
            }

            if (entry.Result != null)
            {
                return(this.CreateOKResult(entry.Result));
            }
            else
            {
                // TODO: Should also be able to handle 204.
                return(this.StatusCode(HttpStatusCode.NotImplemented));
            }
        }
Beispiel #4
0
        private IHttpActionResult ExecuteAction(ODataActionParameters parameters)
        {
            ODataPath odataPath = Request.ODataProperties().Path;
            UnboundActionPathSegment segment = odataPath.Segments[odataPath.Segments.Count - 1] as UnboundActionPathSegment;

            if (segment == null || !_functions.IsRegistered(segment.ActionName))
            {
                return(SetResult("Action not found"));
            }

            Action action = _functions.GetFunction(segment.ActionName) as Action;

            if (action == null)
            {
                return(SetResult("Action not found"));
            }

            QueryParameters queryParameters = new QueryParameters(this);

            queryParameters.Count       = null;
            queryParameters.Request     = Request;
            queryParameters.RequestBody = (string)Request.Properties[PostPatchHandler.RequestContent];
            var result = action.Handler(queryParameters, parameters);

            if (action.ReturnType == typeof(void))
            {
                return(Ok());
            }

            if (result == null)
            {
                return(SetResult("Result is null."));
            }

            if (result is DataObject)
            {
                var entityType = _model.GetEdmEntityType(result.GetType());
                return(SetResult(GetEdmObject(entityType, result, 1, null)));
            }

            if (!(result is string) && result is IEnumerable)
            {
                Type type = null;
                if (result.GetType().IsGenericType)
                {
                    Type[] args = result.GetType().GetGenericArguments();
                    if (args.Length == 1)
                    {
                        type = args[0];
                    }
                }

                if (result.GetType().IsArray)
                {
                    type = result.GetType().GetElementType();
                }

                if (type != null && (type.IsSubclassOf(typeof(DataObject)) || type == typeof(DataObject)))
                {
                    var coll = GetEdmCollection((IEnumerable)result, type, 1, null);
                    return(SetResult(coll));
                }
            }

            return(SetResultPrimitive(result.GetType(), result));
        }