Добавьте общее количество в метаданные, используя JSON API .Net Core. - PullRequest
3 голосов
/ 20 июня 2019

Я использую JSON API .Net Core для создания .Net API.Как добавить total-count в метаданные?enter image description here

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" } }
        };
        }
    }

В этом документе ничего нет.Спасибо.

1 Ответ

1 голос
/ 20 июня 2019

Во-первых, включите опции 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 },
        };
    }
}

Рабочая демонстрация:

* +1012 *enter image description here
...