gpt4 book ai didi

rest - Web Api 2 Odata Controller 发布/放置关系/链接/引用与 Switch 语句

转载 作者:行者123 更新时间:2023-12-01 06:28:50 26 4
gpt4 key购买 nike

在 WebApi 2+ Controller 中使用 OData V3(#ref) 或 V4(#links) 并处理关系:

Switch 语句是唯一的方法吗?

这 2 个资源似乎表明每个导航属性都有一个 switch case 的必要性。

  • Entity Relations in OData V3
  • Entity Relations in OData V4

    switch (navigationProperty)
    {
    case "Supplier":
    // Note: The code for GetKeyFromUri is shown later in this topic.
    var relatedKey = Helpers.GetKeyFromUri<int>(Request, link);
    var supplier = await db.Suppliers.SingleOrDefaultAsync(f => f.Id == relatedKey);
    if (supplier == null)
    {
    return NotFound();
    }

    product.Supplier = supplier;
    break;

    default:
    return StatusCode(HttpStatusCode.NotImplemented);
    }

问题:我是否应该为每个导航属性硬编码一个 switch 语句,如果是这样,有没有办法让这个自动神奇的脚手架?

最佳答案

我有这样的问题 (v4),我通过使用基本 Controller 解决了它 example .有 2 个基本 Controller :用于导航(您的情况)和 CRUD 操作 - 第 3 个是使用示例。代码不是很干净(我一直试图删除不必要的细节)但我认为这个想法很清楚。我不认为这是解决这个问题的最好方法,但我还没有找到任何更简单的方法来通过字符串添加和删除属性。我想知道更优雅的答案。对不起我的英语。

关于rest - Web Api 2 Odata Controller 发布/放置关系/链接/引用与 Switch 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40139268/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com