使用 JSON API .Net Core 在元数据中添加总计数

Add total-count in meta data using JSON API .Net Core

我正在使用 JSON API .Net Core 创建 .Net API。如何在元数据中添加 total-count

public class BaseEntity : Identifiable, IHasMeta
    {

        public string CreatedBy { get; set; }

        public string ModifiedBy { get; set; }

        public Dictionary<string, object> GetMeta(IJsonApiContext context)
        {
            return new Dictionary<string, object> {
            { "copyright", "Copyright Croos" },
            { "authors", new string[] { "Croos" } }
        };
        }
    }

文档中没有任何内容。 谢谢。

首先,启用IncludeTotalRecordCount选项:

public void ConfigureServices(IServiceCollection services)
{
    ... other services

    services.AddJsonApi<AppDbContext>(o =>{
        o.IncludeTotalRecordCount = true;
    });
}

现在我们可以通过 context.PageManager.TotalRecords :

检索总记录
public class Person : Identifiable, IHasMeta
{
    [Attr("name")]
    public string Name { get; set; }

    public Dictionary<string, object> GetMeta(IJsonApiContext context)
    {
        return new Dictionary<string, object> {
            { "total-records", context.PageManager.TotalRecords },
        };
    }
}

一个工作演示: