private static HttpConfiguration CreateConfiguration(IEdmModel model) { HttpConfiguration configuration = new[] { typeof(MainEntityController), typeof(PeopleController), typeof(EnumCustomersController), typeof(CollectionSerializerCustomersController), typeof(PresidentController) }.GetHttpConfiguration(); configuration.MapODataServiceRoute(model); configuration.Formatters.InsertRange(0, ODataMediaTypeFormatters.Create()); return configuration; }
public void GetEntry_UsesRouteModel_ForMultipleModels() { // Model 1 only has Name, Model 2 only has Age ODataModelBuilder builder1 = new ODataModelBuilder(); var personType1 = builder1.EntityType<FormatterPerson>(); personType1.HasKey(p => p.PerId); personType1.Property(p => p.Name); builder1.EntitySet<FormatterPerson>("People").HasIdLink(p => new Uri("http://link/"), false); var model1 = builder1.GetEdmModel(); ODataModelBuilder builder2 = new ODataModelBuilder(); var personType2 = builder2.EntityType<FormatterPerson>(); personType2.HasKey(p => p.PerId); personType2.Property(p => p.Age); builder2.EntitySet<FormatterPerson>("People").HasIdLink(p => new Uri("http://link/"), false); var model2 = builder2.GetEdmModel(); var config = new[] { typeof(PeopleController) }.GetHttpConfiguration(); config.MapODataServiceRoute("OData1", "v1", model1); config.MapODataServiceRoute("OData2", "v2", model2); using (HttpServer host = new HttpServer(config)) using (HttpClient client = new HttpClient(host)) { using (HttpResponseMessage response = client.GetAsync("http://localhost/v1/People(10)").Result) { Assert.True(response.IsSuccessStatusCode); JToken json = JToken.Parse(response.Content.ReadAsStringAsync().Result); // Model 1 has the Name property but not the Age property Assert.NotNull(json["Name"]); Assert.Null(json["Age"]); } using (HttpResponseMessage response = client.GetAsync("http://localhost/v2/People(10)").Result) { Assert.True(response.IsSuccessStatusCode); JToken json = JToken.Parse(response.Content.ReadAsStringAsync().Result); // Model 2 has the Age property but not the Name property Assert.Null(json["Name"]); Assert.NotNull(json["Age"]); } } }
private HttpResponseMessage GetEnumResponse(string acceptHeader) { ODataConventionModelBuilder builder = new ODataConventionModelBuilder(); builder.EntitySet<EnumCustomer>("EnumCustomers"); IEdmModel model = builder.GetEdmModel(); HttpConfiguration configuration = new[] { typeof(EnumCustomersController) }.GetHttpConfiguration(); configuration.MapODataServiceRoute("odata", routePrefix: null, model: model); HttpServer host = new HttpServer(configuration); HttpClient client = new HttpClient(host); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/EnumCustomers"); request.Content = new StringContent( string.Format(@"{{'@odata.type':'#System.Web.OData.Formatter.EnumCustomer', 'ID':0,'Color':'Green, Blue','Colors':['Red','Red, Blue']}}")); request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json"); request.Headers.Accept.ParseAdd(acceptHeader); HttpResponseMessage response = client.SendAsync(request).Result; return response; }