I'm using the JSON API .Net Core to create .Net API. How to add total-count in meta-data?
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" } }
};
}
}
There is nothing about it in the document. Thanks.
Firstly, enable the IncludeTotalRecordCount
options:
public void ConfigureServices(IServiceCollection services)
{
... other services
services.AddJsonApi<AppDbContext>(o =>{
o.IncludeTotalRecordCount = true;
});
}
And now we can retrieve the total records by 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 },
};
}
}
A working demo :