代码之家  ›  专栏  ›  技术社区  ›  si2030

Asp.net使用分页的核心OData似乎无法找到如何获得总记录计数

  •  0
  • si2030  · 技术社区  · 5 年前

    我看了很多问题- here , here this OData教程Asp.Net核心。

    当你做分页时,我的理解是你需要从我正在阅读的内容中指定开始、跳过和项目总数等Asp.net核心OData没有 inlinecount 作为查询选项。

    如果是这样的话,你到底是怎么得到一个页面集的项目总数的?

            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 回复  |  直到 5 年前
        1
  •  0
  •   Tomato32    5 年前

    回复如下: enter image description here

    enter image description here

    访问此链接 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();
                });            
    
            }