接受原始数组作为 OData 操作参数

Accepting primitive array as OData action parameter

我想将 int 的数组作为参数传递给 ASP.NET OData 路由,但无法使其正常工作。

我试过以下方法:

[ODataRoute("IWantMeSomeNumbers/GetForNumbers(numbers={numbers})")]
public IQueryable<IAmAnAppropriateResponse> GetForNumbers(int[] numbers)
{
    // snip
}

[ODataRoute("IWantMeSomeNumbers/GetForNumbers(numbers=[{numbers}])")]
public IQueryable<IAmAnAppropriateResponse> GetForNumbers(int[] numbers)
{
    // snip
}

[ODataRoute("IWantMeSomeNumbers/GetForNumbers(numbers={numbers})")]
public IQueryable<IAmAnAppropriateResponse> GetForNumbers(IEnumerable<int> numbers)
{
    // snip
}

[ODataRoute("IWantMeSomeNumbers/GetForNumbers(numbers=[{numbers}])")]
public IQueryable<IAmAnAppropriateResponse> GetForNumbers(IEnumerable<int> numbers)
{
    // snip
}

[ODataRoute("IWantMeSomeNumbers/GetForNumbers(numbers={numbers})")]
public IQueryable<IAmAnAppropriateResponse> GetForNumbers([FromODataUri] int[] numbers)
{
    // snip
}

[ODataRoute("IWantMeSomeNumbers/GetForNumbers(numbers=[{numbers}])")]
public IQueryable<IAmAnAppropriateResponse> GetForNumbers([FromODataUri] int[] numbers)
{
    // snip
}

[ODataRoute("IWantMeSomeNumbers/GetForNumbers(numbers={numbers})")]
public IQueryable<IAmAnAppropriateResponse> GetForNumbers([FromODataUri] IEnumerable<int> numbers)
{
    // snip
}

[ODataRoute("IWantMeSomeNumbers/GetForNumbers(numbers=[{numbers}])")]
public IQueryable<IAmAnAppropriateResponse> GetForNumbers([FromODataUri] IEnumerable<int> numbers)
{
    // snip
}

[ODataRoute("IWantMeSomeNumbers/GetForNumbers()")]
public IQueryable<IAmAnAppropriateResponse> GetForNumbers(ODataActionParameters parameters)
{
    // snip
}

在每种情况下,我都会收到以下错误:

The path template 'IWantMeSomeNumbers/GetForNumbers(numbers={numbers})' on the action 'GetForNumbers' in controller 'IWantMeSomeNumbers' is not a valid OData path template. Bad Request - Error in query syntax.

谁能告诉我这是否可行,如果可行,我做错了什么??

所以,事实证明我只是被误导了,需要在构建器上定义 EntityType.CollectionParameter<int>

ODataModelBuilder builder = new ODataConventionModelBuilder();
// snip
builder.EntityType<IAmAnAppropriateResponse>().Collection.Function(nameof(IWantMeSomeNumbersController.GetForNumbers))
    .ReturnsCollection<IAmAnAppropriateResponse>()
    .CollectionParameter<int>("numbers");

与以下方法签名配对,一切都按要求工作:

[HttpGet]
[ODataRoute("IWantMeSomeNumbers/GetForNumbers(numbers={numbers})")]
public IQueryable<IAmAnAppropriateResponse> GetForNumbers([FromODataUri] int[] numbers)
{
    // snip
}