currently i use the webapi and odata and want to create a small test project. The route mapping is currently not working correctly.
I use following libs: - Microsoft.AspNetCore.All (2.0.3) - Microsoft.AspNetCore.Odata (7.0.0-beta1) - newest webapi with core2.0
Configure Section:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
//Adding Model class to OData
var builder = new ODataConventionModelBuilder(app.ApplicationServices);
builder.EntitySet<Person>($"{nameof(Person)}s");
//app.
//app.UseMvcWithDefaultRoute();
app.UseMvc(routebuilder =>
{
routebuilder.EnableDependencyInjection();
routebuilder.Filter().Expand().Select().OrderBy().MaxTop(null).Count();
routebuilder.MapODataServiceRoute("odata", "odata", builder.GetEdmModel());
});
}
Controller Section:
public class PersonsController : ODataController
{
private static readonly List<Person> myPersons = new List<Person>
{
new Person {Firstname = "Person1", Lastname= "Test", Id = "p1"},
new Person {Firstname = "Person2", Lastname= "Test", Id = "p2"},
new Person {Firstname= "Person3", Lastname= "Test", Id = "p3"},
new Person {Firstname= "Person4", Lastname= "Test", Id = "p4"},
};
public IQueryable<Person> GetPersons()
{
return myPersons.AsQueryable();
}
public Person GetPerson([FromODataUri] string key)
{
return myPersons.FirstOrDefault(x => x.Id == key);
}
}
Model Section:
public class Person
{
[Key]
public string Id { get; set; }
public string Firstname{ get; set; }
public string Lastname { get; set; }
}
In the odata documentation these calls should work now:
But they aren't. I always get a page not found result from IIS. What i think is that the MapODataServiceRoute call does not set up the routes correctly. Î dont use any default routes in webapi. Any ideas ?
I tried several things out and found a solution to the problem. The order in the config method is important:
This is working:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddOData();
}
this NOT:
public void ConfigureServices(IServiceCollection services)
{
services.AddOData();
services.AddMvc();
}