简体   繁体   中英

Create custom response in .NET Core 5 with OData 8 controller

I'm using OData 8 in .NET Core 5 with the [EnableQuery] attribute. 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).

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:

{ "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": [],

嗨,您遇到了在 OData/WebApi https://github.com/OData/WebApi/issues/2511 中修复的此错误,但似乎尚未迁移到 AspNetCoreOData,存在合并时应允许的拉取请求您继续使用有效的用例。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM