我正在尝试使用OData进行分页,但是似乎返回的数据集没有要查询的完整集的总数。
我已经在一些问题中着眼于SO-here,here,而C#Corner在Asp.Net Core的OData上有this教程。
进行分页时,我的理解是需要指定开始和跳过以及项目总数等。从我的阅读中,Asp.net Core OData没有inlinecount
作为查询选项。
如果是这种情况,那么实际上如何通过页面设置来获得项目总数?
startup.cs中包含以下内容:
app.UseMvc(routeBuilder => {
routeBuilder.EnableDependencyInjection();
routeBuilder.Expand().Select().OrderBy().Filter().MaxTop(null).Count().Expand();
我的控制器中有
[HttpGet("Index", Name = "ClientIndex")]
[EnableQuery()]
public IQueryable<Client> GetClients() {
return _context.Clients;
}
,查询如下:
http://localhost:57500/#clients/clientsList?$top=10&$orderby=ClientNo asc,ClientLastName asc,MobilePhone asc
因此,为了成功实现分页,我需要数据库表中的总记录,但看来您无法获得此值作为响应。.
答案 0 :(得分:0)
使用$ count代替$ inlinecount。
访问此链接https://www.odata.org/getting-started/basic-tutorial/#count,以介绍更多信息,我的朋友。
这里是一个样本。希望对您有帮助,我的朋友:))
安装软件包Microsoft.AspNetCore.OData-版本7.1.0
-模型-
public class Product
{
public int ID { get; set; }
public string Name { get; set; }
}
-控制器-
public class ProductsController : ODataController
{
private List<Product> products = new List<Product>()
{
new Product()
{
ID = 1,
Name = "Bread",
},
new Product()
{
ID = 2,
Name = "Tomato",
},
new Product()
{
ID = 3,
Name = "Lemon",
},new Product()
{
ID = 4,
Name = "Orange",
}
};
[EnableQuery]
public List<Product> Get()
{
return products;
}
}
-启动---
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(option =>
{
option.EnableEndpointRouting = false;
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddOData();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
var builder = new ODataConventionModelBuilder(app.ApplicationServices);
builder.EntitySet<Product>("Products");
app.UseMvc(routeBuilder =>
{
routeBuilder.Select().Expand().Filter().OrderBy().MaxTop(100).Count();
routeBuilder.MapODataServiceRoute("ODataRoute", "odata", builder.GetEdmModel());
routeBuilder.EnableDependencyInjection();
});
}