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


C# HttpRequestMessage.SetODataRouteName方法代碼示例

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


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

示例1: GenerateActionLink_GeneratesLinkWithoutCast_IfEntitySetTypeMatchesActionEntityType

        public void GenerateActionLink_GeneratesLinkWithoutCast_IfEntitySetTypeMatchesActionEntityType()
        {
            // Arrange
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            var cars = builder.EntitySet<Car>("cars");
            var paintAction = cars.EntityType.Action("Paint");

            IEdmModel model = builder.GetEdmModel();
            var carsEdmSet = model.EntityContainers().Single().FindEntitySet("cars");

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

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

            // Act
            Uri link = ActionLinkGenerationConvention.GenerateActionLink(
                new EntityInstanceContext()
                {
                    EdmModel = model,
                    EntitySet = carsEdmSet,
                    EntityType = carsEdmSet.ElementType,
                    Url = request.GetUrlHelper(),
                    EntityInstance = new Car { Model = 2009, Name = "Accord" }
                },
                paintAction);

            Assert.Equal("http://localhost/cars(Model=2009,Name='Accord')/Paint", link.AbsoluteUri);
        }
開發者ID:naulizzang,項目名稱:aspnetwebstack,代碼行數:32,代碼來源:ActionLinkGenerationConventionTest.cs

示例2: 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.EntityContainers().Single().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.MapODataRoute(routeName, null, model);

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

            EntitySetLinkBuilderAnnotation linkBuilder = model.GetEntitySetLinkBuilder(vehiclesEdmEntitySet);

            var serializerContext = new ODataSerializerContext { Model = model, EntitySet = 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.Http.OData.Builder.TestModels.Car/Manufacturer", uri.AbsoluteUri);
        }
開發者ID:brianly,項目名稱:aspnetwebstack,代碼行數:28,代碼來源:NavigationLinksGenerationConventionTest.cs

示例3: GetSampleRequest

 private HttpRequestMessage GetSampleRequest()
 {
     HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/employees");
     HttpConfiguration configuration = new HttpConfiguration();
     string routeName = "Route";
     configuration.Routes.MapODataRoute(routeName, null, GetSampleModel());
     request.SetConfiguration(configuration);
     IEdmEntitySet entitySet = _model.EntityContainers().Single().FindEntitySet("employees");
     request.SetEdmModel(_model);
     request.SetODataPath(new ODataPath(new EntitySetPathSegment(entitySet)));
     request.SetODataRouteName(routeName);
     return request;
 }
開發者ID:quentez,項目名稱:aspnetwebstack,代碼行數:13,代碼來源:FeedTest.cs

示例4: Match

        public override bool Match(HttpRequestMessage request, IHttpRoute route, string parameterName, IDictionary<string, object> values, HttpRouteDirection routeDirection)
        {
            if (routeDirection != HttpRouteDirection.UriResolution)
            {
                return true;
            }

            object odataPathValue;
            if (!values.TryGetValue(ODataRouteConstants.ODataPath, out odataPathValue))
            {
                return false;
            }

            var pathString = odataPathValue as string;
            if (pathString == null) pathString = string.Empty;

            ODataPath path = null;
            try
            {
                path = VersionAwarePathHandler.Parse(request, EdmModel, pathString);
            }
            catch (ODataException e)
            {
                throw new HttpResponseException(request.CreateErrorResponse(HttpStatusCode.NotFound, "Invalid OData path", e));
            }

            if (path != null)
            {
                values[ODataRouteConstants.ODataPath] = path.Segments.First();
                // Set all the properties we need for routing, querying, formatting
                request.SetEdmModel(EdmModel);
                request.SetODataPathHandler(VersionAwarePathHandler);
                request.SetODataPath(path);
                request.SetODataRouteName(RouteName);
                request.SetODataRoutingConventions(RoutingConventions);

                if (!values.ContainsKey(ODataRouteConstants.Controller))
                {
                    // Select controller name using the routing conventions
                    string controllerName = SelectControllerName(path, request);
                    if (controllerName != null)
                    {
                        values[ODataRouteConstants.Controller] = controllerName;
                    }
                }
            }

            return path != null;
        }
開發者ID:jordansjones,項目名稱:ODataVersioning,代碼行數:49,代碼來源:VersionAwareODataPathRouteConstraint.cs

示例5: GenerateActionLink_GeneratesLinkWithoutCast_IfEntitySetTypeDerivesFromActionEntityType

        public void GenerateActionLink_GeneratesLinkWithoutCast_IfEntitySetTypeDerivesFromActionEntityType()
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            var cars = builder.EntitySet<Car>("cars");
            var paintAction = builder.Entity<Vehicle>().Action("Paint");

            IEdmModel model = builder.GetEdmModel();
            var carsEdmSet = model.EntityContainers().Single().FindEntitySet("cars");

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

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

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

            Uri link = ActionLinkGenerationConvention.GenerateActionLink(entityContext, paintAction);

            Assert.Equal("http://localhost/cars(Model=2009,Name='Accord')/Paint", link.AbsoluteUri);
        }
開發者ID:brianly,項目名稱:aspnetwebstack,代碼行數:24,代碼來源:ActionLinkGenerationConventionTest.cs

示例6: GetODataRequest

        private static HttpRequestMessage GetODataRequest(IEdmModel model)
        {
            HttpConfiguration configuration = new HttpConfiguration();
            string routeName = "Route";
            configuration.Routes.MapODataRoute(routeName, null, model);

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost");
            request.SetConfiguration(configuration);
            request.SetODataRouteName(routeName);
            return request;
        }
開發者ID:brianly,項目名稱:aspnetwebstack,代碼行數:11,代碼來源:LinkGenerationHelpersTest.cs

示例7: WhenActionLinksNotManuallyConfigured_ConventionBasedBuilderUsesConventions

        public void WhenActionLinksNotManuallyConfigured_ConventionBasedBuilderUsesConventions()
        {
            // Arrange
            string uriTemplate = "http://server/Movies({0})/Watch";
            Uri expectedUri = new Uri(string.Format(uriTemplate, 1));
            ODataModelBuilder builder = new ODataConventionModelBuilder();
            EntityTypeConfiguration<Movie> movie = builder.EntitySet<Movie>("Movies").EntityType;
            ActionConfiguration watch = movie.Action("Watch");
            IEdmModel model = builder.GetEdmModel();

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://server/Movies");
            HttpConfiguration configuration = new HttpConfiguration();
            string routeName = "Route";
            configuration.Routes.MapODataRoute(routeName, null, model);
            request.SetConfiguration(configuration);
            request.SetODataRouteName(routeName);
            UrlHelper urlHelper = new UrlHelper(request);

            // Act
            IEdmEntityType movieType = model.SchemaElements.OfType<IEdmEntityType>().SingleOrDefault();
            IEdmEntityContainer container = model.SchemaElements.OfType<IEdmEntityContainer>().SingleOrDefault();
            IEdmFunctionImport watchAction = container.FunctionImports().SingleOrDefault();
            IEdmEntitySet entitySet = container.EntitySets().SingleOrDefault();
            EntityInstanceContext<Movie> context = new EntityInstanceContext<Movie>()
            {
                EdmModel = model,
                EntitySet = entitySet,
                EntityType = movieType,
                Url = urlHelper,
                EntityInstance = new Movie { ID = 1, Name = "Avatar" },
                SkipExpensiveAvailabilityChecks = false
            };
            ActionLinkBuilder actionLinkBuilder = model.GetAnnotationValue<ActionLinkBuilder>(watchAction);

            //Assert
            Assert.Equal(expectedUri, watch.GetActionLink()(context));
            Assert.NotNull(actionLinkBuilder);
            Assert.Equal(expectedUri, actionLinkBuilder.BuildActionLink(context));
        }
開發者ID:naulizzang,項目名稱:aspnetwebstack,代碼行數:39,代碼來源:ActionConfigurationTest.cs

示例8: PrimitiveTypesDeserializeAsOData

        public void PrimitiveTypesDeserializeAsOData(Type valueType, object value, MediaTypeHeaderValue mediaType,
            string resourceName)
        {
            string entity = Resources.GetString(resourceName);
            Assert.NotNull(entity);

            object expectedValue = value;

            ODataConventionModelBuilder modelBuilder = new ODataConventionModelBuilder();
            modelBuilder.EntitySet<WorkItem>("WorkItems");
            IEdmModel model = modelBuilder.GetEdmModel();

            object actualValue;

            using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/WorkItems(10)/ID"))
            {
                HttpConfiguration config = new HttpConfiguration();
                config.Routes.MapODataRoute("default", "", model);
                request.SetConfiguration(config);
                request.SetODataRouteName("default");
                request.SetEdmModel(model);

                ODataMediaTypeFormatter formatter = CreateFormatter(request);
                formatter.SupportedMediaTypes.Add(mediaType);

                using (StringContent content = new StringContent(entity))
                {
                    content.Headers.ContentType = mediaType;

                    using (Stream stream = content.ReadAsStreamAsync().Result)
                    {
                        actualValue = formatter.ReadFromStreamAsync(valueType, stream, content,
                            new Mock<IFormatterLogger>().Object).Result;
                    }
                }
            }

            Assert.Equal(expectedValue, actualValue);
        }
開發者ID:brianly,項目名稱:aspnetwebstack,代碼行數:39,代碼來源:PrimitiveTypesTest.cs

示例9: Match

        public virtual bool Match(HttpRequestMessage request, IHttpRoute route, string parameterName, IDictionary<string, object> values, HttpRouteDirection routeDirection)
        {
            if (request == null)
            {
                throw Error.ArgumentNull("request");
            }

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

            if (routeDirection == HttpRouteDirection.UriResolution)
            {
                object odataPathRouteValue;
                if (values.TryGetValue(ODataRouteConstants.ODataPath, out odataPathRouteValue))
                {
                    string odataPath = odataPathRouteValue as string;
                    if (odataPath == null)
                    {
                        // No odataPath means the path is empty; this is necessary for service documents
                        odataPath = String.Empty;
                    }

                    ODataPath path;
                    try
                    {
                        path = PathHandler.Parse(EdmModel, odataPath);
                    }
                    catch (ODataException e)
                    {
                        throw new HttpResponseException(
                            request.CreateErrorResponse(HttpStatusCode.NotFound, SRResources.ODataPathInvalid, e));
                    }

                    if (path != null)
                    {
                        // Set all the properties we need for routing, querying, formatting
                        request.SetEdmModel(EdmModel);
                        request.SetODataPathHandler(PathHandler);
                        request.SetODataPath(path);
                        request.SetODataRouteName(RouteName);
                        request.SetODataRoutingConventions(RoutingConventions);

                        if (!values.ContainsKey(ODataRouteConstants.Controller))
                        {
                            // Select controller name using the routing conventions
                            string controllerName = SelectControllerName(path, request);
                            if (controllerName != null)
                            {
                                values[ODataRouteConstants.Controller] = controllerName;
                            }
                        }

                        return true;
                    }
                }
                return false;
            }
            else
            {
                // This constraint only applies to URI resolution
                return true;
            }
        }
開發者ID:quentez,項目名稱:aspnetwebstack,代碼行數:65,代碼來源:ODataPathRouteConstraint.cs

示例10: GenerateActionLink_GeneratesLinkWithCast_IfEntitySetTypeDoesnotMatchActionEntityType

        public void GenerateActionLink_GeneratesLinkWithCast_IfEntitySetTypeDoesnotMatchActionEntityType()
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            var vehicles = builder.EntitySet<Vehicle>("vehicles");
            var car = builder.Entity<Car>();
            var paintAction = car.Action("Paint");

            IEdmModel model = builder.GetEdmModel();
            var vehiclesEdmSet = model.EntityContainers().Single().FindEntitySet("vehicles");
            var carEdmType = model.FindDeclaredType("System.Web.Http.OData.Builder.TestModels.Car") as IEdmEntityType;

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

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

            Uri link = ActionLinkGenerationConvention.GenerateActionLink(
                new EntityInstanceContext()
                {
                    EdmModel = model,
                    EntitySet = vehiclesEdmSet,
                    EntityType = carEdmType,
                    Url = request.GetUrlHelper(),
                    EntityInstance = new Car { Model = 2009, Name = "Accord" }
                },
                paintAction);

            Assert.Equal("http://localhost/vehicles(Model=2009,Name='Accord')/System.Web.Http.OData.Builder.TestModels.Car/Paint", link.AbsoluteUri);
        }
開發者ID:naulizzang,項目名稱:aspnetwebstack,代碼行數:32,代碼來源:ActionLinkGenerationConventionTest.cs

示例11: GenerateNavigationLink_GeneratesCorrectLink_EvenIfRouteDataPointsToADifferentController

        public void GenerateNavigationLink_GeneratesCorrectLink_EvenIfRouteDataPointsToADifferentController()
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            var orders = builder.AddEntitySet("Orders", builder.AddEntity(typeof(NavigationLinksGenerationConventionTest_Order)));

            IEdmModel model = builder.GetEdmModel();
            var edmEntitySet = model.EntityContainers().Single().EntitySets().Single();

            HttpConfiguration configuration = new HttpConfiguration();
            string routeName = "Route";
            configuration.Routes.MapODataRoute(routeName, null, model);
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost");
            request.SetConfiguration(configuration);
            request.SetODataRouteName(routeName);

            Uri uri =
                NavigationLinksGenerationConvention.GenerateNavigationPropertyLink(
                new EntityInstanceContext
                {
                    EdmModel = model,
                    EntityInstance = new NavigationLinksGenerationConventionTest_Order { ID = 100 },
                    EntitySet = edmEntitySet,
                    EntityType = edmEntitySet.ElementType,
                    Url = request.GetUrlHelper()
                },
                edmEntitySet.ElementType.NavigationProperties().Single(),
                orders,
                includeCast: false);

            Assert.Equal("http://localhost/Orders(100)/Customer", uri.AbsoluteUri);
        }
開發者ID:naulizzang,項目名稱:aspnetwebstack,代碼行數:31,代碼來源:NavigationLinksGenerationConventionTest.cs

示例12: NavigationLinksGenerationConvention_GeneratesLinksWithoutCast_ForBaseProperties

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

            IEdmModel model = builder.GetEdmModel();
            IEdmEntitySet vehiclesEdmEntitySet = model.EntityContainers().Single().FindEntitySet("vehicles");
            IEdmEntityType sportbikeType = model.AssertHasEntityType(typeof(SportBike));
            IEdmNavigationProperty motorcycleManufacturerProperty = sportbikeType.AssertHasNavigationProperty(model, "Manufacturer", typeof(MotorcycleManufacturer), isNullable: true, multiplicity: EdmMultiplicity.ZeroOrOne);

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

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

            EntitySetLinkBuilderAnnotation linkBuilder = model.GetEntitySetLinkBuilder(vehiclesEdmEntitySet);

            Uri uri = linkBuilder.BuildNavigationLink(
                new EntityInstanceContext()
                {
                    EdmModel = model,
                    EntitySet = vehiclesEdmEntitySet,
                    EntityType = sportbikeType,
                    Url = request.GetUrlHelper(),
                    EntityInstance = new Car { Model = 2009, Name = "Ninja" }
                },
                motorcycleManufacturerProperty,
                ODataMetadataLevel.Default);

            Assert.Equal("http://localhost/vehicles(Model=2009,Name='Ninja')/Manufacturer", uri.AbsoluteUri);
        }
開發者ID:naulizzang,項目名稱:aspnetwebstack,代碼行數:35,代碼來源:NavigationLinksGenerationConventionTest.cs


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