Reputation: 464
I can't seem to get my Controller and Mappings configured correctly. I am able to connect just fine if I revert to a standard ApiController and default mapping, but can't connect using the EntityController type and OData mapping. I get a 406 error back, from the server when trying to reference localhost:port/odata/persons My code...
(PS - All of my references and bindings seem to be configured correctly...no errors of any sort.)
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
//Create Entity Data Model
ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();
modelBuilder.EntitySet<Person>("Persons");
//Configure Endpoint
Microsoft.Data.Edm.IEdmModel model = modelBuilder.GetEdmModel();
config.Routes.MapODataRoute("ODataRoute", "odata", model);
}
}
public class PersonsController : EntitySetController<Person, int>
{
static IList<Person> _peeps = new List<Person>()
{
new Person() {ID = 1, FirstName = "Ringo", LastName = "Starr", BirthDate = new DateTime(1940, 7, 7)},
new Person() {ID = 2, FirstName = "John", LastName = "Lennon", BirthDate = new DateTime(1940, 10, 9)},
new Person() {ID = 3, FirstName = "Paul", LastName = "McCartney", BirthDate = new DateTime(1942, 6, 18)},
new Person() {ID = 4, FirstName = "George", LastName = "Harrison", BirthDate = new DateTime(1943, 2, 25)},
};
// GET api/person
[Queryable]
public override IQueryable<Person> Get()
{
return _peeps.AsQueryable();
}
// GET api/person/5
protected override Person GetEntityByKey(int id)
{
return _peeps.FirstOrDefault(p => p.ID == id);
}
}
Upvotes: 0
Views: 1111
Reputation: 57999
This is a very simple scenario that should have worked.
Also you would need to use localhost:port/odata/Persons
(Persons instead of persons). OData Uris are case sensitive.
Upvotes: 2