當前位置: 首頁>>代碼示例>>C#>>正文


C# HttpRequestMessage.ODataProperties方法代碼示例

本文整理匯總了C#中System.Net.Http.HttpRequestMessage.ODataProperties方法的典型用法代碼示例。如果您正苦於以下問題:C# HttpRequestMessage.ODataProperties方法的具體用法?C# HttpRequestMessage.ODataProperties怎麽用?C# HttpRequestMessage.ODataProperties使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Net.Http.HttpRequestMessage的用法示例。


在下文中一共展示了HttpRequestMessage.ODataProperties方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: GetVersion

        private ODataVersion? GetVersion(HttpRequestMessage request)
        {
            // The logic is as follows. We check DataServiceVersion first and if not present we check MaxDataServiceVersion.
            // In order to consider any header valid, it needs to have exactly one valid value, for example a header with
            // value 3.0 will be considered valid, but a header with two values 3.0, 4.0 won't. The user experience is 
            // better if you fail with a 404 here than if you "guess" the version. The moment we see an invalid header 
            // we return a null version. The spec says that in the case of an invalid odata version the service should
            // return 4xx.
            int versionHeaderCount = GetHeaderCount(HttpRequestMessageProperties.ODataServiceVersionHeader, request);
            int maxVersionHeaderCount = GetHeaderCount(HttpRequestMessageProperties.ODataMaxServiceVersionHeader, request);

            if ((versionHeaderCount == 1 && request.ODataProperties().ODataServiceVersion != null))
            {
                return request.ODataProperties().ODataServiceVersion;
            }
            else if ((versionHeaderCount == 0 && maxVersionHeaderCount == 1 &&
               request.ODataProperties().ODataMaxServiceVersion != null))
            {
                return request.ODataProperties().ODataMaxServiceVersion;
            }
            else if (versionHeaderCount == 0 && maxVersionHeaderCount == 0)
            {
                return Version;
            }
            else
            {
                return null;
            }
        }
開發者ID:ahmetgoktas,項目名稱:aspnetwebstack,代碼行數:29,代碼來源:ODataVersionConstraint.cs

示例2: CreateQueryOptions_SetsContextProperties_WithModelAndPath

        public void CreateQueryOptions_SetsContextProperties_WithModelAndPath()
        {
            // Arrange
            ApiController controller = new Mock<ApiController>().Object;
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/Customers");
            controller.Configuration = new HttpConfiguration();
            ODataModelBuilder odataModel = new ODataModelBuilder();
            string setName = typeof(Customer).Name;
            odataModel.EntityType<Customer>().HasKey(c => c.Id);
            odataModel.EntitySet<Customer>(setName);
            IEdmModel model = odataModel.GetEdmModel();
            IEdmEntitySet entitySet = model.EntityContainer.FindEntitySet(setName);
            request.ODataProperties().Model = model;
            request.ODataProperties().Path = new ODataPath(new EntitySetPathSegment(entitySet));
            controller.Request = request;

            // Act
            ODataQueryOptions<Customer> queryOptions =
                EntitySetControllerHelpers.CreateQueryOptions<Customer>(controller);

            // Assert
            Assert.Same(model, queryOptions.Context.Model);
            Assert.Same(entitySet, queryOptions.Context.NavigationSource);
            Assert.Same(typeof(Customer), queryOptions.Context.ElementClrType);
        }
開發者ID:tlycken,項目名稱:aspnetwebstack,代碼行數:25,代碼來源:EntitySetControllerHelpersTest.cs

示例3: PathHandlerGetter_ReturnsDefaultPathHandlerByDefault

        public void PathHandlerGetter_ReturnsDefaultPathHandlerByDefault()
        {
            HttpRequestMessage request = new HttpRequestMessage();
            IEdmModel model = new EdmModel();
            request.ODataProperties().Model = model;

            var pathHandler = request.ODataProperties().PathHandler;

            Assert.NotNull(pathHandler);
            Assert.IsType<DefaultODataPathHandler>(pathHandler);
        }
開發者ID:KevMoore,項目名稱:aspnetwebstack,代碼行數:11,代碼來源:HttpRequestMessageExtensionsTest.cs

示例4: PathHandlerGetter_Returns_PathHandlerSetter

        public void PathHandlerGetter_Returns_PathHandlerSetter()
        {
            HttpRequestMessage request = new HttpRequestMessage();
            IODataPathHandler parser = new Mock<IODataPathHandler>().Object;

            // Act
            request.ODataProperties().PathHandler = parser;

            // Assert
            Assert.Same(parser, request.ODataProperties().PathHandler);
        }
開發者ID:KevMoore,項目名稱:aspnetwebstack,代碼行數:11,代碼來源:HttpRequestMessageExtensionsTest.cs

示例5: SendAsync

        /// <inheritdoc/>
        protected async override Task<HttpResponseMessage> SendAsync(
            HttpRequestMessage request,
            CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw Error.ArgumentNull("request");
            }

            HttpConfiguration configuration = request.GetConfiguration();
            if (configuration == null)
            {
                throw Error.InvalidOperation(SRResources.RequestMustContainConfiguration);
            }

            HttpResponseMessage response = await base.SendAsync(request, cancellationToken);

            // Do not interfere with null responses, we want to buble it up to the top.
            // Do not handle 204 responses as the spec says a 204 response must not include an ETag header
            // unless the request's representation data was saved without any transformation applied to the body
            // (i.e., the resource's new representation data is identical to the representation data received in the
            // PUT request) and the ETag value reflects the new representation.
            // Even in that case returning an ETag is optional and it requires access to the original object which is 
            // not possible with the current architecture, so if the user is interested he can set the ETag in that
            // case by himself on the response.
            if (response == null || !response.IsSuccessStatusCode || response.StatusCode == HttpStatusCode.NoContent)
            {
                return response;
            }

            ODataPath path = request.ODataProperties().Path;
            IEdmModel model = request.ODataProperties().Model;

            IEdmEntityType edmType = GetSingleEntityEntityType(path);
            object value = GetSingleEntityObject(response);

            IEdmEntityTypeReference typeReference = GetTypeReference(model, edmType, value);
            if (typeReference != null)
            {
                EntityInstanceContext context = CreateInstanceContext(typeReference, value);
                context.EdmModel = model;
                context.NavigationSource = path.NavigationSource;
                IETagHandler etagHandler = configuration.GetETagHandler();
                EntityTagHeaderValue etag = CreateETag(context, etagHandler);

                if (etag != null)
                {
                    response.Headers.ETag = etag;
                }
            }

            return response;
        }
開發者ID:chinadragon0515,項目名稱:WebApi,代碼行數:54,代碼來源:ETagMessageHandler.cs

示例6: GetRequest

 private HttpRequestMessage GetRequest(IEdmModel model, IEdmSingleton singleton)
 {
     HttpConfiguration config = new HttpConfiguration();
     config.Routes.MapODataServiceRoute("odata", "odata", model);
     HttpRequestMessage request = new HttpRequestMessage();
     request.SetConfiguration(config);
     request.ODataProperties().PathHandler = new DefaultODataPathHandler();
     request.ODataProperties().RouteName = "odata";
     request.ODataProperties().Model = model;
     request.ODataProperties().Path = new ODataPath(new[] { new SingletonPathSegment(singleton) });
     request.RequestUri = new Uri("http://localhost/odata/Boss");
     return request;
 }
開發者ID:nicholaspei,項目名稱:aspnetwebstack,代碼行數:13,代碼來源:ODataSingletonSerializerTest.cs

示例7: ModelGetter_Returns_ModelSetter

        public void ModelGetter_Returns_ModelSetter()
        {
            // Arrange
            HttpRequestMessage request = new HttpRequestMessage();
            IEdmModel model = new EdmModel();

            // Act
            request.ODataProperties().Model = model;
            IEdmModel newModel = request.ODataProperties().Model;

            // Assert
            Assert.Same(model, newModel);
        }
開發者ID:KevMoore,項目名稱:aspnetwebstack,代碼行數:13,代碼來源:HttpRequestMessageExtensionsTest.cs

示例8: GetSampleRequest

 private HttpRequestMessage GetSampleRequest()
 {
     HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/employees");
     request.ODataProperties().Model = _model;
     HttpConfiguration configuration = new HttpConfiguration();
     string routeName = "Route";
     configuration.Routes.MapODataServiceRoute(routeName, null, _model);
     request.SetConfiguration(configuration);
     IEdmEntitySet entitySet = _model.EntityContainers().Single().FindEntitySet("employees");
     request.ODataProperties().Path = new ODataPath(new EntitySetPathSegment(entitySet));
     request.ODataProperties().RouteName = routeName;
     return request;
 }
開發者ID:ZhaoYngTest01,項目名稱:WebApi,代碼行數:13,代碼來源:EntityTypeTest.cs

示例9: TryMatchMediaType_WithNonRawvalueRequest_DoesntMatchRequest

        public void TryMatchMediaType_WithNonRawvalueRequest_DoesntMatchRequest()
        {
            IEdmModel model = ODataTestUtil.GetEdmModel();
            PropertyAccessPathSegment propertySegment = new PropertyAccessPathSegment((model.GetEdmType(typeof(FormatterPerson)) as IEdmEntityType).FindProperty("Age"));
            ODataPath path = new ODataPath(new EntitySetPathSegment("People"), new KeyValuePathSegment("1"), propertySegment);
            ODataPrimitiveValueMediaTypeMapping mapping = new ODataPrimitiveValueMediaTypeMapping();
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/People(1)/Age/");
            request.ODataProperties().Model = model;
            request.ODataProperties().Path = path;

            double mapResult = mapping.TryMatchMediaType(request);

            Assert.Equal(0, mapResult);
        }
開發者ID:ahmetgoktas,項目名稱:aspnetwebstack,代碼行數:14,代碼來源:ODataRawValueMediaTypeMappingTests.cs

示例10: TryMatchMediaTypeWithBinaryRawValueMatchesRequest

        public void TryMatchMediaTypeWithBinaryRawValueMatchesRequest()
        {
            IEdmModel model = GetBinaryModel();
            PropertyAccessPathSegment propertySegment = new PropertyAccessPathSegment((model.GetEdmType(typeof(RawValueEntity)) as IEdmEntityType).FindProperty("BinaryProperty"));
            ODataPath path = new ODataPath(new EntitySetPathSegment("RawValue"), new KeyValuePathSegment("1"), propertySegment, new ValuePathSegment());
            ODataBinaryValueMediaTypeMapping mapping = new ODataBinaryValueMediaTypeMapping();
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/RawValue(1)/BinaryProperty/$value");
            request.ODataProperties().Model = model;
            request.ODataProperties().Path = path;

            double mapResult = mapping.TryMatchMediaType(request);

            Assert.Equal(1.0, mapResult);
        }
開發者ID:huangw-t,項目名稱:aspnetwebstack,代碼行數:14,代碼來源:ODataRawValueMediaTypeMappingTests.cs

示例11: NavigationLinksGenerationConvention_GeneratesLinksWithCast_ForDerivedProperties

        public void NavigationLinksGenerationConvention_GeneratesLinksWithCast_ForDerivedProperties()
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            builder.EntitySet<Vehicle>("vehicles");
            builder.EntitySet<Manufacturer>("manufacturers");

            IEdmModel model = builder.GetEdmModel();
            IEdmEntitySet vehiclesEdmEntitySet = model.EntityContainer.FindEntitySet("vehicles");
            IEdmEntityType carType = model.AssertHasEntityType(typeof(Car));
            IEdmNavigationProperty carManufacturerProperty = carType.AssertHasNavigationProperty(model, "Manufacturer", typeof(CarManufacturer), isNullable: true, multiplicity: EdmMultiplicity.ZeroOrOne);

            HttpConfiguration configuration = new HttpConfiguration();
            string routeName = "Route";
            configuration.Routes.MapODataServiceRoute(routeName, null, model);

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost");
            request.SetConfiguration(configuration);
            request.ODataProperties().RouteName = routeName;

            NavigationSourceLinkBuilderAnnotation linkBuilder = model.GetNavigationSourceLinkBuilder(vehiclesEdmEntitySet);

            var serializerContext = new ODataSerializerContext { Model = model, NavigationSource = vehiclesEdmEntitySet, Url = request.GetUrlHelper() };
            var entityContext = new EntityInstanceContext(serializerContext, carType.AsReference(), new Car { Model = 2009, Name = "Accord" });

            Uri uri = linkBuilder.BuildNavigationLink(entityContext, carManufacturerProperty, ODataMetadataLevel.Default);

            Assert.Equal("http://localhost/vehicles(Model=2009,Name='Accord')/System.Web.OData.Builder.TestModels.Car/Manufacturer", uri.AbsoluteUri);
        }
開發者ID:nicholaspei,項目名稱:aspnetwebstack,代碼行數:28,代碼來源:NavigationLinksGenerationConventionTest.cs

示例12: Convention_GeneratesUri_ForActionBoundToEntity

        public void Convention_GeneratesUri_ForActionBoundToEntity()
        {
            // Arrange
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            builder.EntitySet<Customer>("Customers");
            var action = builder.EntityType<Customer>().Action("MyAction");
            action.Parameter<string>("param");
            IEdmModel model = builder.GetEdmModel();

            // Act
            HttpConfiguration configuration = new HttpConfiguration();
            configuration.MapODataServiceRoute("odata", "odata", model);

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost:123");
            request.SetConfiguration(configuration);
            request.ODataProperties().RouteName = "odata";

            IEdmEntitySet customers = model.EntityContainer.FindEntitySet("Customers");
            var edmType = model.SchemaElements.OfType<IEdmEntityType>().First(e => e.Name == "Customer");
            var serializerContext = new ODataSerializerContext { Model = model, NavigationSource = customers, Url = request.GetUrlHelper() };
            var entityContext = new EntityInstanceContext(serializerContext, edmType.AsReference(), new Customer { Id = 109 });

            // Assert
            var edmAction = model.SchemaElements.OfType<IEdmAction>().First(f => f.Name == "MyAction");
            Assert.NotNull(edmAction);

            ActionLinkBuilder actionLinkBuilder = model.GetActionLinkBuilder(edmAction);
            Uri link = actionLinkBuilder.BuildActionLink(entityContext);

            Assert.Equal("http://localhost:123/odata/Customers(109)/Default.MyAction", link.AbsoluteUri);
        }
開發者ID:chinadragon0515,項目名稱:WebApi,代碼行數:31,代碼來源:ActionLinkGenerationConventionTest.cs

示例13: SendAsync

        /// <inheritdoc />
        protected async override Task<HttpResponseMessage> SendAsync(
            HttpRequestMessage request,
            CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw Error.ArgumentNull("request");
            }

            HttpResponseMessage response = await base.SendAsync(request, cancellationToken);

            // This message handler is intended for helping with queries that return a null value, for example in a
            // get request for a particular entity on an entity set, for a single valued navigation property or for
            // a structural property of a given entity. The only case in which a data modification request will result
            // in a 204 response status code, is when a primitive property is set to null through a PUT request to the
            // property URL and in that case, the user can return the right status code himself.
            ObjectContent content = response == null ? null : response.Content as ObjectContent;
            if (request.Method == HttpMethod.Get && content != null && content.Value == null &&
                response.StatusCode == HttpStatusCode.OK)
            {
                HttpStatusCode? newStatusCode = GetUpdatedResponseStatusCodeOrNull(request.ODataProperties().Path);
                if (newStatusCode.HasValue)
                {
                    response = request.CreateResponse(newStatusCode.Value);
                }
            }

            return response;
        }
開發者ID:ahmetgoktas,項目名稱:aspnetwebstack,代碼行數:30,代碼來源:ODataNullValueMessageHandler.cs

示例14: CreateHttpRequestMessage

        private static HttpRequestMessage CreateHttpRequestMessage(HttpActionDescriptor actionDescriptor, ODataRoute oDataRoute, HttpConfiguration httpConfig)
        {
            Contract.Requires(httpConfig != null);
            Contract.Requires(oDataRoute != null);
            Contract.Requires(httpConfig != null);
            Contract.Ensures(Contract.Result<HttpRequestMessage>() != null);

            Contract.Assume(oDataRoute.Constraints != null);

            var httpRequestMessage = new HttpRequestMessage(actionDescriptor.SupportedHttpMethods.First(), "http://any/");

            var requestContext = new HttpRequestContext
            {
                Configuration = httpConfig
            };
            httpRequestMessage.SetConfiguration(httpConfig);
            httpRequestMessage.SetRequestContext(requestContext);

            var httpRequestMessageProperties = httpRequestMessage.ODataProperties();
            Contract.Assume(httpRequestMessageProperties != null);
            httpRequestMessageProperties.Model = oDataRoute.GetEdmModel();
            httpRequestMessageProperties.RouteName = oDataRoute.GetODataPathRouteConstraint().RouteName;
            httpRequestMessageProperties.RoutingConventions = oDataRoute.GetODataPathRouteConstraint().RoutingConventions;
            httpRequestMessageProperties.PathHandler = oDataRoute.GetODataPathRouteConstraint().PathHandler;
            return httpRequestMessage;
        }
開發者ID:bigred8982,項目名稱:Swashbuckle.OData,代碼行數:26,代碼來源:AttributeRouteStrategy.cs

示例15: GetModel

        public override IEdmModel GetModel(Type elementClrType, HttpRequestMessage request,
            HttpActionDescriptor actionDescriptor)
        {
            // Get model for the request
            IEdmModel model = request.ODataProperties().Model;

            if (model == null)
            {
                // user has not configured anything or has registered a model without the element type
                // let's create one just for this type and cache it in the action descriptor
                model = actionDescriptor.Properties.GetOrAdd("System.Web.OData.Model+" + elementClrType.FullName, _ =>
                {
                    ODataConventionModelBuilder builder =
                        new ODataConventionModelBuilder(actionDescriptor.Configuration, isQueryCompositionMode: true);
                    builder.EnableLowerCamelCase();
                    EntityTypeConfiguration entityTypeConfiguration = builder.AddEntityType(elementClrType);
                    builder.AddEntitySet(elementClrType.Name, entityTypeConfiguration);
                    IEdmModel edmModel = builder.GetEdmModel();
                    Contract.Assert(edmModel != null);
                    return edmModel;
                }) as IEdmModel;
            }

            Contract.Assert(model != null);
            return model;
        }
開發者ID:tanjinfu,項目名稱:WebApiODataSamples,代碼行數:26,代碼來源:MyEnableQueryAttribute.cs


注:本文中的System.Net.Http.HttpRequestMessage.ODataProperties方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。