【发布时间】:2026-01-07 18:10:02
【问题描述】:
根据此处的文档 ...
http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v3/odata-actions
...
将动作绑定到实体集
在前面的示例中,操作绑定到单个实体: 客户对单个产品进行评分。您还可以将操作绑定到 实体的集合。只需进行以下更改:
在 EDM 中,将操作添加到实体的 Collection 属性中。
var rateAllProducts = builder.Entity().Collection.Action("RateAllProducts");在里面 控制器方法,省略key参数。
[HttpPost] public int RateAllProducts(ODataActionParameters 参数) { // .... }
为什么当我这样做时它不起作用......
发票参考:
public class InvoiceReference
{
public string InvoiceNumber { get; set; }
public int SupplierId { get; set; }
}
动作设置:
var getByRefs = Builder.EntityType<SIHead>().Collection.Action("ByRefs");
getByRefs.CollectionParameter<InvoiceReference>("refs");
getByRefs.ReturnsCollectionFromEntitySet<SIHead>("SIHead");
控制器中的动作方法:
[HttpPost]
[EnableQuery]
[ODataRoute("ByRefs")]
public async Task<IHttpActionResult> ByRefs(ODataActionParameters p)
{
var refs = p["refs"] as InvoiceReference[];
// exception p is null
}
发布的示例 json 内容:
[
{
"InvoiceNumber": "5100011759|9800006622",
"SupplierId": 2
},
{
"InvoiceNumber": "5100012624|9800006635",
"SupplierId": 2
},
{
"InvoiceNumber": "5100012625|9800006636",
"SupplierId": 2
}
]
在我看来,要么我错过了什么,要么 OData 坏了。
【问题讨论】:
-
尝试发布如下内容,您需要在帖子内容中指定带有JSON格式值的参数名称,这应该可以,{“refs”:[{“InvoiceNumber”:“5100011759|9800006622 ", "SupplierId": 2 }, { "InvoiceNumber": "5100012624|9800006635", "SupplierId": 2 }, { "InvoiceNumber": "5100012625|9800006636", "SupplierId": 2 } ] }
-
是的,我做到了,不起作用......我也在 github 上提出了这个问题:github.com/OData/WebApi/issues/758