Exemplo n.º 1
0
        /// <summary>
        /// Selects the controller for OData requests.
        /// </summary>
        /// <param name="odataPath">The OData path.</param>
        /// <param name="request">The request.</param>
        /// <returns>
        ///   <c>null</c> if the request isn't handled by this convention; otherwise, the name of the selected controller
        /// </returns>
        public virtual string SelectController(ODataPath odataPath, HttpRequestMessage request)
        {
            if (odataPath == null)
            {
                throw Error.ArgumentNull("odataPath");
            }

            if (request == null)
            {
                throw Error.ArgumentNull("request");
            }

            // entity set
            EntitySetPathSegment entitySetSegment = odataPath.Segments.FirstOrDefault() as EntitySetPathSegment;

            if (entitySetSegment != null)
            {
                return(entitySetSegment.EntitySetName);
            }

            // singleton
            SingletonPathSegment singletonSegment = odataPath.Segments.FirstOrDefault() as SingletonPathSegment;

            if (singletonSegment != null)
            {
                return(singletonSegment.SingletonName);
            }

            return(null);
        }
        /// <inheritdoc/>
        public override string SelectAction(ODataPath odataPath, HttpControllerContext controllerContext,
                                            ILookup <string, HttpActionDescriptor> actionMap)
        {
            if (odataPath == null)
            {
                throw Error.ArgumentNull("odataPath");
            }

            if (controllerContext == null)
            {
                throw Error.ArgumentNull("controllerContext");
            }

            if (actionMap == null)
            {
                throw Error.ArgumentNull("actionMap");
            }

            if (odataPath.PathTemplate == "~/singleton")
            {
                SingletonPathSegment singletonSegment = (SingletonPathSegment)odataPath.Segments[0];
                string httpMethodName = GetActionNamePrefix(controllerContext.Request.Method);

                if (httpMethodName != null)
                {
                    // e.g. Try Get{SingletonName} first, then fallback on Get action name
                    return(actionMap.FindMatchingAction(
                               httpMethodName + singletonSegment.Singleton.Name,
                               httpMethodName));
                }
            }
            else if (odataPath.PathTemplate == "~/singleton/cast")
            {
                SingletonPathSegment singletonSegment = (SingletonPathSegment)odataPath.Segments[0];
                IEdmEntityType       entityType       = (IEdmEntityType)odataPath.EdmType;
                string httpMethodName = GetActionNamePrefix(controllerContext.Request.Method);

                if (httpMethodName != null)
                {
                    // e.g. Try Get{SingletonName}From{EntityTypeName} first, then fallback on Get action name
                    return(actionMap.FindMatchingAction(
                               httpMethodName + singletonSegment.Singleton.Name + "From" + entityType.Name,
                               httpMethodName + "From" + entityType.Name));
                }
            }

            return(null);
        }