简体   繁体   English

ASP.NET Core 3.1 OData PageResult 序列化

[英]ASP.NET Core 3.1 OData PageResult Serialization

The PageResult response is missing the nextPageLink and count values when being serialized through System.Text.Json (ASP.NET Core 3 default) for the following result: PageResult响应在通过System.Text.Json (ASP.NET Core 3 默认)序列化时缺少nextPageLinkcount数值,结果如下:

[ApiController]
[Route("api/[controller]")]
public class ValuesController : ControllerBase
{
  [HttpGet]
  public IActionResult Get()
  {
    var items = new List<string> { "value" };

    var pageResult = new Microsoft.AspNet.OData.PageResult<string>(items, new Uri("http://localhost/foo"), items.Count);

    return Ok(pageResult);
  }
}

However when I use the Newtonsoft.Json formatter , the result is correct:但是,当我使用Newtonsoft.Json格式化程序时,结果是正确的:

public void ConfigureServices(IServiceCollection services)
{
  services.AddControllers().AddNewtonsoftJson();
}

Result with System.Text.Json : System.Text.Json结果:

[
    "value"
]

Result with Newtonsoft.Json : Newtonsoft.Json结果:

{
    "items": [
        "value"
    ],
    "nextPageLink": "http://localhost/foo",
    "count": 1
}

Any idea what the reason is for this behaviour?知道这种行为的原因是什么吗?

On re-reading the question, this doesn't directly answer why the OP's question of why one formatter does and another does not cause the correct output, but may provide some places for the OP to start looking...在重新阅读问题时,这并没有直接回答为什么 OP 的问题,即为什么一个格式化程序会这样做而另一个不会导致正确的输出,但可能会为 OP 提供一些开始寻找的地方......

I have also seen interesting behaviour when using OData and Asp.Net Core, it's very difficult to decipher from the online documentation what you should do these days, as most of the docs seem to out of date or for earlier versions of OData.在使用 OData 和 Asp.Net Core 时,我也看到了一些有趣的行为,很难从在线文档中解读这些天你应该做什么,因为大多数文档似乎已经过时或适用于 OData 的早期版本。 For example, you'll see some references to GetInlineCount() in some documtation, but it appears to have been removed from the API.例如,您会在某些文档中看到对 GetInlineCount() 的一些引用,但它似乎已从 API 中删除。

For your query, I think you have two options Firstly, change your return type to be IQueryable then specify the attribute [EnableQuery(PageSize=10)] .对于您的查询,我认为您有两个选择首先,将您的返回类型更改为 IQueryable 然后指定属性[EnableQuery(PageSize=10)]

Alternatively, keeping what what you have, and using the PageResult object:或者,保留你所拥有的,并使用PageResult对象:

return new PageResult<object>(listOfObjects as IEnumerable<object>, Request.GetNextPageLink(5), Request.ODataFeature().TotalCount);

You can solve the Request.ODataFeature().TotalCount not returning a value by setting the Request.ODataFeature().TotalCountFunc function as follows...您可以通过如下设置 Request.ODataFeature().TotalCountFunc 函数来解决 Request.ODataFeature().TotalCount 不返回值的问题...

Request.ODataFeature().TotalCountFunc = () => listOfobjects.Count();

[ApiController]
[Route("api/[controller]")]
public class ValuesController : ODataController
{
  [HttpGet]
  [EnableQuery(PageSize=5)]
  public IActionResult Get()
  {
    var items = new List<string> { "value1","value2","value3","value4","value5"  };
 
    return Ok(items);
  }
}

Extend your controller with ODataController instead of ControllerBase .使用ODataController而不是ControllerBase扩展您的ControllerBase [EnableQuery] attribute needs to be added to the method. [EnableQuery]属性需要添加到方法中。 You can define a PageSize if you're doing server side paging.如果您要进行服务器端分页,则可以定义PageSize

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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