Esempio n. 1
0
        public void GenerateSelfLinkWithCast_Works()
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            var vehicles = builder.AddEntitySet("cars", builder.AddEntity(typeof(Car)));

            IEdmModel     model            = builder.GetEdmModel();
            IEdmEntitySet carsEdmEntitySet = 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);

            string idLink =
                SelfLinksGenerationConvention.GenerateSelfLink(
                    vehicles,
                    new EntityInstanceContext()
            {
                EdmModel       = model,
                EntitySet      = carsEdmEntitySet,
                EntityType     = carsEdmEntitySet.ElementType,
                Url            = request.GetUrlHelper(),
                EntityInstance = new Car {
                    Model = 2009, Name = "Accord"
                }
            },
                    includeCast: true);

            Assert.Equal("http://localhost/cars(Model=2009,Name='Accord')/System.Web.Http.OData.Builder.TestModels.Car", idLink);
        }
        public void GenerateSelfLinkWithoutCast_Works()
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            var vehicles = builder.AddEntitySet("cars", builder.AddEntity(typeof(Car)));

            IEdmModel     model            = builder.GetEdmModel();
            IEdmEntitySet carsEdmEntitySet = model.EntityContainers().Single().EntitySets().Single();

            HttpConfiguration configuration = new HttpConfiguration();

            configuration.Routes.MapHttpRoute(ODataRouteNames.GetById, "{controller}({id})");

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

            request.Properties[HttpPropertyKeys.HttpConfigurationKey] = configuration;
            request.Properties[HttpPropertyKeys.HttpRouteDataKey]     = new HttpRouteData(new HttpRoute());

            Uri uri =
                SelfLinksGenerationConvention.GenerateSelfLink(
                    vehicles,
                    new EntityInstanceContext(model, carsEdmEntitySet, carsEdmEntitySet.ElementType, request.GetUrlHelper(), new Car {
                Model = 2009, Name = "Accord"
            }),
                    includeCast: false);

            Assert.Equal("http://localhost/cars(Model=2009,Name='Accord')", uri.AbsoluteUri);
        }
Esempio n. 3
0
        public void GenerateSelfLinkWithCast_Works()
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            var vehicles = builder.AddEntitySet("cars", builder.AddEntity(typeof(Car)));

            IEdmModel     model            = builder.GetEdmModel();
            IEdmEntitySet carsEdmEntitySet = model.EntityContainers().Single().EntitySets().Single();

            HttpConfiguration configuration = new HttpConfiguration();

            configuration.EnableOData(model);

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

            request.Properties[HttpPropertyKeys.HttpConfigurationKey] = configuration;
            request.Properties[HttpPropertyKeys.HttpRouteDataKey]     = new HttpRouteData(new HttpRoute());

            Uri uri =
                SelfLinksGenerationConvention.GenerateSelfLink(
                    vehicles,
                    new EntityInstanceContext()
            {
                EdmModel       = model,
                EntitySet      = carsEdmEntitySet,
                EntityType     = carsEdmEntitySet.ElementType,
                UrlHelper      = request.GetUrlHelper(),
                PathHandler    = new DefaultODataPathHandler(model),
                EntityInstance = new Car {
                    Model = 2009, Name = "Accord"
                }
            },
                    includeCast: true);

            Assert.Equal("http://localhost/cars(Model=2009,Name='Accord')/System.Web.Http.OData.Builder.TestModels.Car", uri.AbsoluteUri);
        }