예제 #1
0
        /// <summary>
        /// Generates a navigation link following the OData URL conventions for the entity represented by <paramref name="resourceContext"/> and the given
        /// navigation property.
        /// </summary>
        /// <param name="resourceContext">The <see cref="ResourceContext"/> representing the entity for which the navigation link needs to be generated.</param>
        /// <param name="navigationProperty">The EDM navigation property.</param>
        /// <param name="includeCast">Represents whether the generated link should have a cast segment representing a type cast.</param>
        /// <returns>The navigation link following the OData URL conventions.</returns>
        public static Uri GenerateNavigationPropertyLink(this ResourceContext resourceContext,
                                                         IEdmNavigationProperty navigationProperty, bool includeCast)
        {
            if (resourceContext == null)
            {
                throw Error.ArgumentNull("resourceContext");
            }
            if (resourceContext.InternalUrlHelper == null)
            {
                throw Error.Argument("resourceContext", SRResources.UrlHelperNull, typeof(ResourceContext).Name);
            }

            IList <ODataPathSegment> navigationPathSegments = resourceContext.GenerateBaseODataPathSegments();

            if (includeCast)
            {
                navigationPathSegments.Add(new TypeSegment(resourceContext.StructuredType, navigationSource: null));
            }

            navigationPathSegments.Add(new NavigationPropertySegment(navigationProperty, navigationSource: null));

            string link = resourceContext.InternalUrlHelper.CreateODataLink(navigationPathSegments);

            if (link == null)
            {
                return(null);
            }

            return(new Uri(link));
        }
예제 #2
0
        /// <summary>
        /// Generates a self link following the OData URL conventions for the entity represented by <paramref name="resourceContext"/>.
        /// </summary>
        /// <param name="resourceContext">The <see cref="ResourceContext"/> representing the entity for which the self link needs to be generated.</param>
        /// <param name="includeCast">Represents whether the generated link should have a cast segment representing a type cast.</param>
        /// <returns>The self link following the OData URL conventions.</returns>
        public static Uri GenerateSelfLink(this ResourceContext resourceContext, bool includeCast)
        {
            if (resourceContext == null)
            {
                throw Error.ArgumentNull("resourceContext");
            }

            if (resourceContext.InternalUrlHelper == null)
            {
                throw Error.Argument("resourceContext", SRResources.UrlHelperNull, typeof(ResourceContext).Name);
            }

            IList <ODataPathSegment> idLinkPathSegments = resourceContext.GenerateBaseODataPathSegments();

            bool isSameType = resourceContext.StructuredType == resourceContext.NavigationSource.EntityType();

            if (includeCast && !isSameType)
            {
                idLinkPathSegments.Add(new TypeSegment(resourceContext.StructuredType, navigationSource: null));
            }

            string idLink = resourceContext.InternalUrlHelper.CreateODataLink(idLinkPathSegments);

            if (idLink == null)
            {
                return(null);
            }

            return(new Uri(idLink));
        }
예제 #3
0
        internal static Uri GenerateFunctionLink(this ResourceContext resourceContext,
                                                 IEdmTypeReference bindingParameterType, IEdmOperation function,
                                                 IEnumerable <string> parameterNames)
        {
            IList <ODataPathSegment> functionPathSegments = resourceContext.GenerateBaseODataPathSegments();

            // generate link with cast if the navigation source type doesn't match the entity type the function is bound to.
            if (resourceContext.NavigationSource.EntityType() != bindingParameterType.Definition)
            {
                functionPathSegments.Add(new TypeSegment(bindingParameterType.Definition, null));
            }

            IList <OperationSegmentParameter> parameters = new List <OperationSegmentParameter>();

            // skip the binding parameter
            foreach (string param in parameterNames.Skip(1))
            {
                string value = "@" + param;
                parameters.Add(new OperationSegmentParameter(param, new ConstantNode(value, value)));
            }

            OperationSegment segment = new OperationSegment(new[] { function }, parameters, null);

            functionPathSegments.Add(segment);

            string functionLink = resourceContext.InternalUrlHelper.CreateODataLink(functionPathSegments);

            return(functionLink == null ? null : new Uri(functionLink));
        }
예제 #4
0
        internal static Uri GenerateActionLink(this ResourceContext resourceContext,
                                               IEdmTypeReference bindingParameterType, IEdmOperation action)
        {
            Contract.Assert(resourceContext != null);
            if (resourceContext.NavigationSource is IEdmContainedEntitySet)
            {
                return(null);
            }

            IList <ODataPathSegment> actionPathSegments = resourceContext.GenerateBaseODataPathSegments();

            // generate link with cast if the navigation source doesn't match the entity type the action is bound to.
            if (resourceContext.NavigationSource.EntityType() != bindingParameterType.Definition)
            {
                actionPathSegments.Add(new TypeSegment((IEdmEntityType)bindingParameterType.Definition, null));
                // entity set can be null
            }

            OperationSegment operationSegment = new OperationSegment(new[] { action }, null);

            actionPathSegments.Add(operationSegment);

            string actionLink = resourceContext.InternalUrlHelper.CreateODataLink(actionPathSegments);

            return(actionLink == null ? null : new Uri(actionLink));
        }