无法正确配置Odata服务

时间:2013-05-22 17:00:59

标签: asp.net-web-api odata

我似乎无法正确配置我的Controller和Mappings。如果我恢复到标准ApiController和默认映射,我能够连接正常,但无法使用EntityController类型和OData映射进行连接。我在尝试引用localhost时从服务器返回406错误:port / odata / persons我的代码......

(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);
    }
}

1 个答案:

答案 0 :(得分:2)

这是一个应该有效的非常简单的场景。

此外,您需要使用localhost:port/odata/Persons(人而不是人)。 OData Uris区分大小写。