【问题标题】:Add total-count in meta data using JSON API .Net Core使用 JSON API .Net Core 在元数据中添加总数
【发布时间】:2019-06-20 05:05:30
【问题描述】:

我正在使用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" } }
        };
        }
    }

文档中没有关于它的内容。 谢谢。

【问题讨论】:

    标签: .net asp.net-core json-api


    【解决方案1】:

    首先,启用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 },
            };
        }
    }
    

    工作演示:

    【讨论】:

      猜你喜欢
      • 2019-05-30
      • 1970-01-01
      • 1970-01-01
      • 2016-10-29
      • 1970-01-01
      • 1970-01-01
      • 2019-08-14
      • 1970-01-01
      • 2019-04-23
      相关资源
      最近更新 更多