Example #1
0
        /// <summary>
        /// Build an ODataUriParser
        /// </summary>
        /// <param name="model">Model to use for metadata binding.</param>
        /// <param name="serviceRoot">Absolute URI of the service root.</param>
        /// <param name="uri">Absolute or relative URI to be parsed.</param>
        /// <param name="container">The optional dependency injection container to get related services for URI parsing.</param>
        public ODataUriParser(IEdmModel model, Uri serviceRoot, Uri uri, IServiceProvider container)
        {
            ExceptionUtils.CheckArgumentNotNull(uri, "uri");

            if (serviceRoot == null)
            {
                throw new ODataException(ODataErrorStrings.UriParser_NeedServiceRootForThisOverload);
            }

            if (!serviceRoot.IsAbsoluteUri)
            {
                throw new ODataException(ODataErrorStrings.UriParser_UriMustBeAbsolute(serviceRoot));
            }

            this.configuration = new ODataUriParserConfiguration(model, container);
            this.serviceRoot   = UriUtils.EnsureTaillingSlash(serviceRoot);
            this.uri           = uri.IsAbsoluteUri ? uri : UriUtils.UriToAbsoluteUri(this.ServiceRoot, uri);
            this.queryOptions  = QueryOptionUtils.ParseQueryOptions(this.uri);
        }
Example #2
0
        private bool TryComputeIdFromParent(out Uri uri)
        {
            try
            {
                ODataConventionalResourceMetadataBuilder parent = this.ParentMetadataBuilder as ODataConventionalResourceMetadataBuilder;
                if (parent != null && parent != this)
                {
                    // Get the parent canonical url
                    uri = parent.GetCanonicalUrl();

                    if (uri != null)
                    {
                        // And append cast (if needed).
                        // A cast segment if the navigation property is defined on a type derived from the type expected.
                        IODataResourceTypeContext typeContext = parent.ResourceMetadataContext.TypeContext;

                        if (parent.ResourceMetadataContext.ActualResourceTypeName != typeContext.ExpectedResourceTypeName)
                        {
                            // Do not append type cast if we know that the navigation property is in base type, not in derived type.
                            ODataResourceTypeContext.ODataResourceTypeContextWithModel typeContextWithModel =
                                typeContext as ODataResourceTypeContext.ODataResourceTypeContextWithModel;
                            if (typeContextWithModel == null ||
                                typeContextWithModel.ExpectedResourceType.FindProperty(
                                    this.ResourceMetadataContext.TypeContext.NavigationSourceName) == null)
                            {
                                uri = new Uri(UriUtils.EnsureTaillingSlash(uri),
                                              parent.ResourceMetadataContext.ActualResourceTypeName);
                            }
                        }

                        return(true);
                    }
                }
            }
            catch (ODataException)
            {
            }

            uri = null;
            return(false);
        }