我似乎无法正确配置我的控制器和映射。如果我恢复到标准 ApiController 和默认映射,我可以正常连接,但无法使用 EntityController 类型和 OData 映射进行连接。尝试引用 localhost:port/odata/persons 我的代码时,我从服务器收到 406 错误...
(PS - 我所有的引用和绑定似乎都配置正确......没有任何错误。)
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);
}
}