简体   繁体   English

使用 OData 8 控制器在 .NET Core 5 中创建自定义响应

[英]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.

相关问题 如何在 ASP.NET Core 控制器中返回自定义 HTTP 响应? - How do I return a custom HTTP response in an ASP.NET Core Controller? 将自定义UserManager注入Controller Net Core 2.1 - Inject Custom UserManager To Controller Net Core 2.1 .NET 核心:用于 controller 身份验证的自定义承载过滤器 - .NET Core : custom bearer filter for controller authentication 在Asp.Net Core 2中是否有与Response.Redirect(“〜/ Controller /”)相同的功能? - Is there an equivalent to Response.Redirect(“~/Controller/”) in Asp.Net Core 2? 迁移到 Asp.net 内核 3 更改了 controller 响应? - Migrating to Asp.net core 3 changed the controller response? OData(创建自定义)查询选项 - OData (create custom) query option 如何在 ASP.Net Core 中实现自定义 controller 动作选择? - How to implement custom controller action selection in ASP.Net Core? 如何在asp net core中将自定义授权过滤器添加到方法不是controller? - How to add custom authorization filter to method not controller in asp net core? Asp.net 内核 Controller 的自定义模型绑定问题 - Custom ModelBinding Issues With Asp.net Core Controller 如何在一个控制器中创建几个url使用.net Core - How to create few url in one controller use .net Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM