December 21, 2024

Pagination in ASP.NET Core

Hey there! For long I wanted to write the next post in the series I started some time ago about Mindgaze.AspNetCore library. Due to high workload at the office and things I do in the free time I didn’t manage to write new blog posts. Now that I have to stay home due to COVID-19, there are time gaps which can be filled by posting.

Getting back to business, it comes natural right now to speak a little about pagination. Pagination is a really important subject for a business application. It ensures the data is nicely presented to the user and that the app is not exhausted with requests that retrieve large data sets. I certainly believe that 90% of data queries sent to the database should be paginated because of the performance implications. What do you think of this? 😊

The Mindgaze.AspNetCore has builtin capabilities for this which are implemented straight into EntityController. Basically, if you followed the article series so far, you should already have a controller that is capable of pagination. Pretty cool 😉

What you need to know is just the query parameters you need to send to enable that. So here they are:

  1. page – quite obvious, this tells the controller to retrieve the page number you supply
  2. recordsPerPage – self-explanatory, this one tells how many records to bring per page. The number of items returned might be lower than this parameter. It defaults to 50

Putting it into practice

Ok, let’s now use Postman to get records from the bathroomProducts endpoint defined in the sample application. A simple GET with no parameters will return the records with default pagination options (page 1, 50 records per page).

You should notice the pager object that is returned with each request. This will tell you important info about paging context. It will also generate URIs for next, previous, first and last pages. Feel free to use that if it’s useful in your app.

Ok let’s say we want page number 2:

Here you can notice that we have no records for page 2, but we get the previous page URL. Let’s now also use the recordsPerPage parameter and set it to 2:

This time we get one item (because we’re on last page) and we can observe that we still have the previous page URL filled but also the first page URL. Let’s check page number 1:

Now we got the first 2 items and the URLs filled correctly. Nice, I really find it useful. You can also combine this with filtering and pagination will work fine!

Conclusion

The time has come to get the paycheck. I mean to draw a conclusion 😅. This article is about showing how easy is to have pagination in ASP.NET Core and how neat it’s working. It can also be combined with filtering to achieve better results. I surely hope this is useful for you as it is for me in my personal projects!

Nuget: https://www.nuget.org/packages/Mindgaze.AspNetCore/

Gitlab: https://gitlab.com/afivan/mindgaze-aspnetcore

Thanks for reading, I hope you found this article useful and interesting. If you have any suggestions don’t hesitate to contact me. If you found my content useful please consider a small donation. Any support is greatly appreciated! Cheers  😉

afivan

Enthusiast adventurer, software developer with a high sense of creativity, discipline and achievement. I like to travel, I like music and outdoor sports. Because I have a broken ligament, I prefer safer activities like running or biking. In a couple of years, my ambition is to become a good technical lead with entrepreneurial mindset. From a personal point of view, I’d like to establish my own family, so I’ll have lots of things to do, there’s never time to get bored 😂

View all posts by afivan →