【问题标题】:Can't get Odata service configured correctly无法正确配置 Odata 服务
【发布时间】:2019-05-17 04:14:11
【问题描述】:

我似乎无法正确配置我的控制器和映射。如果我恢复到标准 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);
    }
}

【问题讨论】:

    标签: asp.net-web-api odata


    【解决方案1】:

    这是一个非常简单的场景,应该可行。

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

    【讨论】:

    • 套管是问题所在。谢谢!
    • 如果你能告诉我原因,我会给你双倍积分,因为对所有神圣事物的热爱,路线是区分大小写的。
    • OData 规范要求实体集名称区分大小写。很不幸。我认为有一个工作项可以为未来版本的 Web API 启用不区分大小写的 OData 路由。
    • 令人震惊的是他们错过了...无论如何...这里有一个解决方法。虽然很明显,把每一个排列都放进去会非常烦人。我确信通过覆盖某些类有更好的方法,但现在这又快又脏…… modelBuilder.EntitySet("Persons"); modelBuilder.EntitySet("persons");
    猜你喜欢
    • 2017-08-02
    • 2020-10-16
    • 2021-03-31
    • 1970-01-01
    • 1970-01-01
    • 2021-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多