[英]Create custom response in .NET Core 5 with OData 8 controller
I'm using OData 8 in .NET Core 5 with the [EnableQuery]
attribute.我在 .NET Core 5 中使用 OData 8 和[EnableQuery]
属性。 Problem is when I want to return Problem()
or BadRequest("Some message")
, the API always returns some default OData BadRequest
and never the message I wanted (only when [EnableQuery]
attribute is there).问题是当我想返回Problem()
或BadRequest("Some message")
,API 总是返回一些默认的 OData BadRequest
而从不返回我想要的消息(仅当[EnableQuery]
属性存在时)。
Example:例子:
[HttpGet]
[EnableQuery(EnsureStableOrdering = false )]
public IActionResult GetList(ODataQueryOptions<List> queryOptions)
{
if (queryOptions.Filter == null)
{
return BadRequest( "Filter is required for this endpoind!" );
}
try
{
queryOptions.Filter.Validator = new OdataCustomFilterValidator();
this.BaseValidateQueryOptions(queryOptions);
}
catch (ODataException ex)
{
return this.BadRequest(ex.Message);
}
IQueryable<List> list = this._service.GetList();
return this.Ok(list);
}
So in the above example, if the code gets to the first IF, i do not recieve this message but ALWAYS the same Odata error:因此,在上面的示例中,如果代码到达第一个 IF,我不会收到此消息,但始终会出现相同的 Odata 错误:
{ "error": { "code": "", "message": "The query specified in the URI is not valid. The requested resource is not a collection. Query options $filter, $orderby, $count, $skip, and $top can be applied only on collections.", "details": [], { "error": { "code": "", "message": "URI 中指定的查询无效。请求的资源不是集合。查询选项 $filter, $orderby, $count, $skip,并且 $top 只能应用于集合。", "details": [],
嗨,您遇到了在 OData/WebApi https://github.com/OData/WebApi/issues/2511 中修复的此错误,但似乎尚未迁移到 AspNetCoreOData,存在合并时应允许的拉取请求您继续使用有效的用例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.