【问题标题】:ASP.NET Core OData Action With Two Parameters具有两个参数的 ASP.NET Core OData 操作
【发布时间】:2020-06-08 16:52:31
【问题描述】:

我有此操作方法可用于 OData:

[HttpPost]
[ODataRoute("({id})/Replace")]
public Blog Replace([FromODataUri] int id, Blog blog)

这会响应/odata/Blogs(1)/Replace 上的 POST 请求。 除了 blog 参数从不从 POST 绑定的部分外,它正在工作,也就是说,它不为空,但具有所有属性的默认值。如果我将[FromBody] 添加到参数中,它将变为空。我也尝试使用ODataActionParameters 类型的参数,但它始终为空。 这是我模型的相关部分:

var replace = builder.EntitySet<Blog>("Blogs").EntityType.Action("Replace");
//replace.Parameter<int>("id").Required();  //this can be added or not, it doesn't matter
replace.EntityParameter<Blog>("blog").Required();
replace.Returns<int>();

我在某处读到 OData 操作不能有两个参数,但我不确定。我需要存在id 参数,因此我需要为此找到解决方案。 我正在使用 ASP.NET Core 3.1 和所有包的最新版本。 我做错了什么?

【问题讨论】:

    标签: asp.net-core odata


    【解决方案1】:

    结果很简单:

    1. 参数声明错误:实体集的名称是“blogs”,而不是“blog”
    2. 我发布了“博客”实体的 JSON,但我必须修改它以包含在参数“博客”中,如下所示:

      { “博客”: { “博客 ID”:1, > } }

    这解决了我的问题。

    【讨论】:

      猜你喜欢
      • 2019-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多