예제 #1
0
        internal static bool ResolveContentIdReferenceInRequestUrl(HttpRequestMessage request)
        {
            ContentIdRecord referencedContentIdRecord = GetReferencedContentIdEntity(request);

            if (referencedContentIdRecord == null)
            {
                return(false);
            }

            // Save the initial Uri (eg http://localhost/odata/$46/Messages ) before the Content-ID references are replaced.
            request.SaveInitialChangeSetRequestUri();

            string originalUri = request.RequestUri.OriginalString;
            string refKey      = "$" + referencedContentIdRecord.ContentId;
            int    ichRef      = originalUri.IndexOf(refKey);

            Contract.Assert(ichRef >= 0);

            // As location headers MUST be absolute URL's, we can ignore everything
            // before the $content-id while resolving it.
            string updatedUri = referencedContentIdRecord.Location + originalUri.Substring(ichRef + refKey.Length);

            request.RequestUri = new Uri(updatedUri);
            return(true);
        }
예제 #2
0
        public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
        {
            ContentIdRecord contentIdRecord = ContentIdHelper.GetReferencedContentIdEntity(actionContext.Request);

            if (contentIdRecord == null)
            {
                // Value doesn't exist
                return(false);
            }

            if (bindingContext.ModelType.IsAssignableFrom(contentIdRecord.EntityType))
            {
                bindingContext.Model = contentIdRecord.Entity;
                return(true);
            }
            else
            {
                string errorMessage = string.Format("Content-ID reference ${0} contains an entity of type {1}, which could not be converted to type {2}.",
                                                    contentIdRecord.ContentId,
                                                    contentIdRecord.EntityType,
                                                    bindingContext.ModelType);
                bindingContext.ModelState.AddModelError(bindingContext.ModelName, errorMessage);
                return(false);
            }
        }
예제 #3
0
        public static bool ContentIdReferenceToEntity(this HttpRequestMessage request, string reference, out object referencedEntity)
        {
            referencedEntity = null;
            int contentId;

            if (!ContentIdHelper.TryParseContentIdReference(reference, out contentId))
            {
                return(false);
            }

            ChangeSetContext changeSetContext = request.GetChangeSetContext();

            if (changeSetContext != null)
            {
                ContentIdRecord contentIdRecord = changeSetContext.GetContentIdRecord(contentId);
                if (contentIdRecord != null)
                {
                    referencedEntity = contentIdRecord.Entity;
                }
            }

            return(referencedEntity != null);
        }