In my previous post in C# Corner, we saw how to get RSS feeds from C# Corner site and display the data in a Blazor project. We saw all the post by an author, all featured posts, all latest posts, and the top read posts.

In this post, we will see the RSS feeds from the C# Corner site with pagination. We will see ten rows at a time on a page and we can have the previous, next, first and last, buttons to navigate the data as our wish. We will provide all the posts by an author, featured articles list, latest posts (all types), latest articles, latest blogs, and top read articles.

If you are new to Blazor framework, please refer to the below articles to get started with Blazor.
We can create a new Blazor project using Visual Studio 2017 (I am using free community edition). Currently, there are three types of templates available for Blazor. We can choose Blazor (ASP.NET Core hosted) template.
C# Corner RSS Feeds in Blazor with Pagination
Our solution will be ready shortly. Please note that there are three projects created in our solution - “Client”, “Server”, and “Shared”.
C# Corner RSS Feeds in Blazor with Pagination

By default, Blazor created many files in these three projects. We can remove all the unwanted files like “Counter.cshtml”, “FetchData.cshtml”, “SurveyPrompt.cshtml” from Client project and “SampleDataController.cs” file from Server project and remove “WeatherForecast.cs” file from shared project too.

Create a new “Pagination” folder in “Shared” project and add “PagedResultBase” abstract class inside this folder.
PagedResultBase.cs
  1. namespace BlazorPagination.Shared.Pagination
  2. {
  3. public abstract class PagedResultBase
  4. {
  5. public int CurrentPage { get; set; }
  6. public int PageCount { get; set; }
  7. public int PageSize { get; set; }
  8. public int RowCount { get; set; }
  9. }
  10. }

This class will be used as base class for other pagination classes. This will contain properties like “CurrentPage”, ”PageCount”, “PageSize” and “RowCount

We can add one more class PagedResult” inside “Pagination” folder. Please note this will be a generic class.
PagedResult.cs
  1. using System.Collections.Generic;
  2. namespace BlazorPagination.Shared.Pagination
  3. {
  4. public class PagedResult<T> : PagedResultBase where T : class
  5. {
  6. public IList<T> Results { get; set; }
  7. public PagedResult()
  8. {
  9. Results = new List<T>();
  10. }
  11. }
  12. }

Create a new “Models” folder in “Shared” project and add a “Feed” class file inside this folder. We will add two classes, “AuthorPosts” and “Feed” inside this class file.

Feed.cs
  1. using BlazorPagination.Shared.Pagination;
  2. using System;
  3. namespace BlazorPagination.Shared.Models
  4. {
  5. public class AuthorPosts
  6. {
  7. public string AuthorName { get; set; }
  8. public PagedResult<Feed> Feeds { get; set; }
  9. }
  10. public class Feed
  11. {
  12. public string Link { get; set; }
  13. public string Title { get; set; }
  14. public string FeedType { get; set; }
  15. public string Author { get; set; }
  16. public string Content { get; set; }
  17. public DateTime PubDate { get; set; }
  18. public string PublishDate { get; set; }
  19. public Feed()
  20. {
  21. Link = "";
  22. Title = "";
  23. FeedType = "";
  24. Author = "";
  25. Content = "";
  26. PubDate = DateTime.Today;
  27. PublishDate = DateTime.Today.ToString("dd-MMM-yyyy");
  28. }
  29. }
  30. }

We have used above “PagedResult” generic type inside this "AuthorPosts” class.

We can create a new folder “Extensions” inside “Server” project and add a static class “PagedResultExtensions” inside this folder.

PagedResultExtensions.cs
  1. using BlazorPagination.Shared.Pagination;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace BlazorPagination.Server.Extensions
  6. {
  7. public static class PagedResultExtensions
  8. {
  9. public static PagedResult<T> GetPagedResult<T>(this IEnumerable<T> query, int page, int pageSize) where T : class
  10. {
  11. var result = new PagedResult<T>
  12. {
  13. CurrentPage = page,
  14. PageSize = pageSize,
  15. RowCount = query.Count()
  16. };
  17. var pageCount = (double)result.RowCount / pageSize;
  18. result.PageCount = (int)Math.Ceiling(pageCount);
  19. var skip = (page - 1) * pageSize;
  20. result.Results = query.Skip(skip).Take(pageSize).ToList();
  21. return result;
  22. }
  23. }
  24. }

We have added a static “GetPagedResult” method in this class. We will use this static method as an extension method later in this post.

We can create a new API Controller “FeedsController.cs” in “Server” project.
Add below code to “FeedsController.cs” to get all posts by an author.
  1. readonly CultureInfo culture = new CultureInfo("en-US");
  2. [Route("allpostsbyauthor/{authorId}/{page}")]
  3. [HttpGet]
  4. public AuthorPosts GetPage(int page, string authorId)
  5. {
  6. AuthorPosts authorPosts = new AuthorPosts();
  7. try
  8. {
  9. XDocument doc = XDocument.Load("https://www.c-sharpcorner.com/members/" + authorId + "/rss");
  10. var entries = from item in doc.Root.Descendants().First(i => i.Name.LocalName == "channel").Elements().Where(i =>
  11. i.Name.LocalName == "item")
  12. select new Feed
  13. {
  14. Content = item.Elements().First(i => i.Name.LocalName == "description").Value,
  15. Link = (item.Elements().First(i => i.Name.LocalName == "link").Value).StartsWith("/") ? "https://www.c-sharpcorner.com" + item.Elements().First(i => i.Name.LocalName == "link").Value : item.Elements().First(i => i.Name.LocalName == "link").Value,
  16. PubDate = Convert.ToDateTime(item.Elements().First(i => i.Name.LocalName == "pubDate").Value, culture),
  17. PublishDate = Convert.ToDateTime(item.Elements().First(i => i.Name.LocalName == "pubDate").Value, culture).ToString("dd-MMM-yyyy"),
  18. Title = item.Elements().First(i => i.Name.LocalName == "title").Value,
  19. FeedType = (item.Elements().First(i => i.Name.LocalName == "link").Value).ToLowerInvariant().Contains("blog") ? "Blog" : (item.Elements().First(i => i.Name.LocalName == "link").Value).ToLowerInvariant().Contains("news") ? "News" : "Article",
  20. Author = item.Elements().First(i => i.Name.LocalName == "author").Value
  21. };
  22. authorPosts.AuthorName = entries.FirstOrDefault().Author;
  23. authorPosts.Feeds = entries.OrderByDescending(o => o.PubDate).GetPagedResult(page, 10);
  24. return authorPosts;
  25. }
  26. catch
  27. {
  28. authorPosts.AuthorName = "NOT FOUND!";
  29. PagedResult<Feed> feeds = new PagedResult<Feed>();
  30. Feed feed = new Feed();
  31. authorPosts.Feeds = feeds;
  32. return authorPosts;
  33. }
  34. }

Code for featured articles.

  1. [Route("featuredarticles/{page}")]
  2. [HttpGet]
  3. public PagedResult<Feed> Featured(int page)
  4. {
  5. try
  6. {
  7. XDocument doc = XDocument.Load("https://www.c-sharpcorner.com/rss/featuredarticles.aspx");
  8. var entries = from item in doc.Root.Descendants().First(i => i.Name.LocalName == "channel").Elements().Where(i =>
  9. i.Name.LocalName == "item")
  10. select new Feed
  11. {
  12. Content = item.Elements().First(i => i.Name.LocalName == "description").Value,
  13. Link = (item.Elements().First(i => i.Name.LocalName == "link").Value).StartsWith("/") ? "https://www.c-sharpcorner.com" + item.Elements().First(i => i.Name.LocalName == "link").Value : item.Elements().First(i => i.Name.LocalName == "link").Value,
  14. PubDate = Convert.ToDateTime(item.Elements().First(i => i.Name.LocalName == "pubDate").Value, culture),
  15. PublishDate = Convert.ToDateTime(item.Elements().First(i => i.Name.LocalName == "pubDate").Value, culture).ToString("dd-MMM-yyyy"),
  16. Title = item.Elements().First(i => i.Name.LocalName == "title").Value,
  17. FeedType = (item.Elements().First(i => i.Name.LocalName == "link").Value).ToLowerInvariant().Contains("blog") ? "Blog" : (item.Elements().First(i => i.Name.LocalName == "link").Value).ToLowerInvariant().Contains("news") ? "News" : "Article",
  18. Author = item.Elements().First(i => i.Name.LocalName == "author").Value
  19. };
  20. return entries.OrderByDescending(o => o.PubDate).GetPagedResult(page, 10);
  21. }
  22. catch
  23. {
  24. PagedResult<Feed> feeds = new PagedResult<Feed>();
  25. return feeds;
  26. }
  27. }

Code for latest posts (All Types).

  1. [Route("latestallposts/{page}")]
  2. [HttpGet]
  3. public PagedResult<Feed> LatestAllPosts(int page)
  4. {
  5. try
  6. {
  7. XDocument doc = XDocument.Load("https://www.c-sharpcorner.com/rss/latestcontentall.aspx");
  8. var entries = from item in doc.Root.Descendants().First(i => i.Name.LocalName == "channel").Elements().Where(i =>
  9. i.Name.LocalName == "item")
  10. select new Feed
  11. {
  12. Content = item.Elements().First(i => i.Name.LocalName == "description").Value,
  13. Link = (item.Elements().First(i => i.Name.LocalName == "link").Value).StartsWith("/") ? "https://www.c-sharpcorner.com" + item.Elements().First(i => i.Name.LocalName == "link").Value : item.Elements().First(i => i.Name.LocalName == "link").Value,
  14. PubDate = Convert.ToDateTime(item.Elements().First(i => i.Name.LocalName == "pubDate").Value, culture),
  15. PublishDate = Convert.ToDateTime(item.Elements().First(i => i.Name.LocalName == "pubDate").Value, culture).ToString("dd-MMM-yyyy"),
  16. Title = item.Elements().First(i => i.Name.LocalName == "title").Value,
  17. FeedType = (item.Elements().First(i => i.Name.LocalName == "link").Value).ToLowerInvariant().Contains("blog") ? "Blog" : (item.Elements().First(i => i.Name.LocalName == "link").Value).ToLowerInvariant().Contains("news") ? "News" : "Article",
  18. Author = item.Elements().First(i => i.Name.LocalName == "author").Value
  19. };
  20. return entries.OrderByDescending(o => o.PubDate).GetPagedResult(page, 10);
  21. }
  22. catch
  23. {
  24. PagedResult<Feed> feeds = new PagedResult<Feed>();
  25. return feeds;
  26. }
  27. }

Code for latest articles.

  1. [Route("latestarticles/{page}")]
  2. [HttpGet]
  3. public PagedResult<Feed> LatestArticles(int page)
  4. {
  5. try
  6. {
  7. XDocument doc = XDocument.Load("https://www.c-sharpcorner.com/rss/latestarticles.aspx");
  8. var entries = from item in doc.Root.Descendants().First(i => i.Name.LocalName == "channel").Elements().Where(i =>
  9. i.Name.LocalName == "item")
  10. select new Feed
  11. {
  12. Content = item.Elements().First(i => i.Name.LocalName == "description").Value,
  13. Link = (item.Elements().First(i => i.Name.LocalName == "link").Value).StartsWith("/") ? "https://www.c-sharpcorner.com" + item.Elements().First(i => i.Name.LocalName == "link").Value : item.Elements().First(i => i.Name.LocalName == "link").Value,
  14. PubDate = Convert.ToDateTime(item.Elements().First(i => i.Name.LocalName == "pubDate").Value, culture),
  15. PublishDate = Convert.ToDateTime(item.Elements().First(i => i.Name.LocalName == "pubDate").Value, culture).ToString("dd-MMM-yyyy"),
  16. Title = item.Elements().First(i => i.Name.LocalName == "title").Value,
  17. FeedType = "Article",
  18. Author = item.Elements().First(i => i.Name.LocalName == "author").Value
  19. };
  20. return entries.OrderByDescending(o => o.PubDate).GetPagedResult(page, 10);
  21. }
  22. catch
  23. {
  24. PagedResult<Feed> feeds = new PagedResult<Feed>();
  25. return feeds;
  26. }
  27. }

Code for latest blogs.

  1. [Route("latestblogs/{page}")]
  2. [HttpGet]
  3. public PagedResult<Feed> LatestBlogs(int page)
  4. {
  5. try
  6. {
  7. XDocument doc = XDocument.Load("https://www.c-sharpcorner.com/rss/blogs.aspx");
  8. var entries = from item in doc.Root.Descendants().First(i => i.Name.LocalName == "channel").Elements().Where(i =>
  9. i.Name.LocalName == "item")
  10. select new Feed
  11. {
  12. Content = item.Elements().First(i => i.Name.LocalName == "description").Value,
  13. Link = (item.Elements().First(i => i.Name.LocalName == "link").Value).StartsWith("/") ? "https://www.c-sharpcorner.com" + item.Elements().First(i => i.Name.LocalName == "link").Value : item.Elements().First(i => i.Name.LocalName == "link").Value,
  14. PubDate = Convert.ToDateTime(item.Elements().First(i => i.Name.LocalName == "pubDate").Value, culture),
  15. PublishDate = Convert.ToDateTime(item.Elements().First(i => i.Name.LocalName == "pubDate").Value, culture).ToString("dd-MMM-yyyy"),
  16. Title = item.Elements().First(i => i.Name.LocalName == "title").Value,
  17. FeedType = "Article",
  18. Author = item.Elements().First(i => i.Name.LocalName == "author").Value
  19. };
  20. return entries.OrderByDescending(o => o.PubDate).GetPagedResult(page, 10);
  21. }
  22. catch
  23. {
  24. PagedResult<Feed> feeds = new PagedResult<Feed>();
  25. return feeds;
  26. }
  27. }

Code for the top read posts

  1. [Route("topreadposts/{page}")]
  2. [HttpGet]
  3. public PagedResult<Feed> TopReadPosts(int page)
  4. {
  5. try
  6. {
  7. XDocument doc = XDocument.Load("https://www.c-sharpcorner.com/rss/toparticles.aspx");
  8. var entries = from item in doc.Root.Descendants().First(i => i.Name.LocalName == "channel").Elements().Where(i =>
  9. i.Name.LocalName == "item")
  10. select new Feed
  11. {
  12. Content = item.Elements().First(i => i.Name.LocalName == "description").Value,
  13. Link = (item.Elements().First(i => i.Name.LocalName == "link").Value).StartsWith("/") ? "https://www.c-sharpcorner.com" + item.Elements().First(i => i.Name.LocalName == "link").Value : item.Elements().First(i => i.Name.LocalName == "link").Value,
  14. PubDate = Convert.ToDateTime(item.Elements().First(i => i.Name.LocalName == "pubDate").Value, culture),
  15. PublishDate = Convert.ToDateTime(item.Elements().First(i => i.Name.LocalName == "pubDate").Value, culture).ToString("dd-MMM-yyyy"),
  16. Title = item.Elements().First(i => i.Name.LocalName == "title").Value,
  17. FeedType = "Article",
  18. Author = item.Elements().First(i => i.Name.LocalName == "author").Value
  19. };
  20. return entries.GetPagedResult(page, 10);
  21. }
  22. catch
  23. {
  24. PagedResult<Feed> feeds = new PagedResult<Feed>();
  25. return feeds;
  26. }
  27. }

We can go to “Client” project and add a Pager component inside the “Shared” folder so that, this component can be shared among all other pages in Blazor.

Pager.cshtml
  1. @using BlazorPagination.Shared.Pagination
  2. @if (Result != null)
  3. {
  4. <div class="row">
  5. <div class="col-md-8 col-sm-8">
  6. @if (Result.PageCount > 1)
  7. {
  8. <ul class="pagination pull-right">
  9. <li><button type="button" onclick="@(() => PagerButtonClicked(1))" class="btn">«</button></li>
  10. @for (var i = StartIndex; i <= FinishIndex; i++)
  11. {
  12. var currentIndex = i;
  13. @if (i == Result.CurrentPage)
  14. {
  15. <li><span class="btn">@i</span></li>
  16. }
  17. else
  18. {
  19. <li><button type="button" onclick="@(() => PagerButtonClicked(currentIndex))" class="btn">@i</button></li>
  20. }
  21. }
  22. <li><button type="button" onclick="@(() => PagerButtonClicked(Result.PageCount))" class="btn">»</button></li>
  23. </ul>
  24. }
  25. </div>
  26. </div>
  27. }
  28. @functions {
  29. [Parameter]
  30. protected PagedResultBase Result { get; set; }
  31. [Parameter]
  32. protected Action<int> PageChanged { get; set; }
  33. protected int StartIndex { get; private set; } = 0;
  34. protected int FinishIndex { get; private set; } = 0;
  35. protected override void OnParametersSet()
  36. {
  37. StartIndex = Math.Max(Result.CurrentPage - 5, 1);
  38. FinishIndex = Math.Min(Result.CurrentPage + 5, Result.PageCount);
  39. base.OnParametersSet();
  40. }
  41. protected void PagerButtonClicked(int page)
  42. {
  43. PageChanged?.Invoke(page);
  44. }
  45. }

We can modify the “NavMenu.cshtml” razor file.

NavMenu.cshtml
  1. <div class="top-row pl-4 navbar navbar-dark">
  2. <a class="navbar-brand" href="">Blazor Pagination</a>
  3. <button class="navbar-toggler" onclick=@ToggleNavMenu>
  4. <span class="navbar-toggler-icon"></span>
  5. </button>
  6. </div>
  7. <div class=@(collapseNavMenu ? "collapse" : null) onclick=@ToggleNavMenu>
  8. <ul class="nav flex-column">
  9. <li class="nav-item px-3">
  10. <NavLink class="nav-link" href="" Match=NavLinkMatch.All>
  11. <span class="oi oi-home" aria-hidden="true"></span> Home
  12. </NavLink>
  13. </li>
  14. <li class="nav-item px-3">
  15. <NavLink class="nav-link" href="/page">
  16. <span class="oi oi-list-rich" aria-hidden="true"></span> All Posts by an Author
  17. </NavLink>
  18. </li>
  19. <li class="nav-item px-3">
  20. <NavLink class="nav-link" href="/featuredarticles/1">
  21. <span class="oi oi-list-rich" aria-hidden="true"></span> Featured Articles
  22. </NavLink>
  23. </li>
  24. <li class="nav-item px-3">
  25. <NavLink class="nav-link" href="/latestallposts/1">
  26. <span class="oi oi-list-rich" aria-hidden="true"></span> Latest Posts
  27. </NavLink>
  28. </li>
  29. <li class="nav-item px-3">
  30. <NavLink class="nav-link" href="/latestarticles/1">
  31. <span class="oi oi-list-rich" aria-hidden="true"></span> Latest Articles
  32. </NavLink>
  33. </li>
  34. <li class="nav-item px-3">
  35. <NavLink class="nav-link" href="/latestblogs/1">
  36. <span class="oi oi-list-rich" aria-hidden="true"></span> Latest Blogs
  37. </NavLink>
  38. </li>
  39. <li class="nav-item px-3">
  40. <NavLink class="nav-link" href="/topreadposts/1">
  41. <span class="oi oi-list-rich" aria-hidden="true"></span> Top Read Posts
  42. </NavLink>
  43. </li>
  44. </ul>
  45. </div>
  46. @functions {
  47. bool collapseNavMenu = true;
  48. void ToggleNavMenu()
  49. {
  50. collapseNavMenu = !collapseNavMenu;
  51. }
  52. }

AllPostsByAuthor.cshtml

  1. @using BlazorPagination.Shared.Models
  2. @using BlazorPagination.Shared.Pagination
  3. @page "/page"
  4. @page "/page/{Page}"
  5. @inject HttpClient Http
  6. @inject Microsoft.AspNetCore.Blazor.Services.IUriHelper UriHelper
  7. <h4>C# Corner All Post Details by an Author</h4>
  8. <input placeholder="Enter Author Id" bind="@authorId" />
  9. <input type="button" class="btn btn-default" onclick="@(async () => await GetFeeds())" value="Get Posts" />
  10. <br />
  11. <br />
  12. <p><b>@author</b></p>
  13. @if (feeds == null || feeds.Results.Count == 0)
  14. {
  15. if (!pageLoaded && authorId != "" && author != "Invalid Author Id!")
  16. {
  17. <p><em>Loading...</em></p>
  18. }
  19. }
  20. else
  21. {
  22. if (!author.ToUpperInvariant().Contains("NOT FOUND") && author != "Invalid Author Id!" && authorId != "")
  23. {
  24. counter = (feeds.CurrentPage - 1) * 10;
  25. <table class="table table-striped">
  26. <thead>
  27. <tr>
  28. <th>Sl.No.</th>
  29. <th>Post Title</th>
  30. <th>Post Type</th>
  31. <th>Content</th>
  32. <th>Publish Date</th>
  33. </tr>
  34. </thead>
  35. <tbody>
  36. @foreach (var feed in feeds.Results)
  37. {
  38. counter++;
  39. <tr>
  40. <td>@counter</td>
  41. <td><NavLink href[email protected] target="_blank">@feed.Title</NavLink></td>
  42. <td>@feed.FeedType</td>
  43. <td>@feed.Content</td>
  44. <td>@feed.PublishDate</td>
  45. </tr>
  46. }
  47. </tbody>
  48. </table>
  49. <Pager Result=@feeds PageChanged=@PagerPageChanged />
  50. }
  51. }
  52. @functions{
  53. PagedResult<Feed> feeds;
  54. AuthorPosts authorPosts;
  55. string author;
  56. int counter;
  57. string @authorId;
  58. bool pageLoaded;
  59. [Parameter]
  60. protected string Page { get; set; } = "1";
  61. protected override void OnInit()
  62. {
  63. authorId = "";
  64. pageLoaded = true;
  65. author = "";
  66. }
  67. protected override async Task OnParametersSetAsync()
  68. {
  69. await LoadFeeds(int.Parse(Page));
  70. }
  71. private async Task GetFeeds()
  72. {
  73. feeds = null;
  74. pageLoaded = false;
  75. await LoadFeeds(1);
  76. }
  77. private async Task LoadFeeds(int page)
  78. {
  79. author = "";
  80. if (!pageLoaded)
  81. {
  82. if (authorId != "")
  83. {
  84. authorPosts = await Http.GetJsonAsync<AuthorPosts>("/api/feeds/allpostsbyauthor/" + authorId + "/" + page.ToString());
  85. if (authorPosts != null)
  86. {
  87. if (authorPosts.AuthorName.ToUpperInvariant().Contains("NOT FOUND"))
  88. {
  89. author = "Invalid Author Id!";
  90. }
  91. else
  92. {
  93. author = "Author Name : " + authorPosts.AuthorName;
  94. feeds = authorPosts.Feeds;
  95. }
  96. }
  97. }
  98. else
  99. {
  100. author = "Author Id should not be blank";
  101. }
  102. }
  103. }
  104. protected void PagerPageChanged(int page)
  105. {
  106. feeds = null;
  107. UriHelper.NavigateTo("/page/" + page);
  108. }
  109. }

FeaturedArticles.cshtml

  1. @using BlazorPagination.Shared.Models
  2. @using BlazorPagination.Shared.Pagination
  3. @page "/featuredarticles/{Page}"
  4. @inject HttpClient Http
  5. @inject Microsoft.AspNetCore.Blazor.Services.IUriHelper UriHelper
  6. <h4>C# Corner Featured Articles List</h4>
  7. @if (feeds == null)
  8. {
  9. <p><em>Loading...</em></p>
  10. }
  11. else
  12. {
  13. counter = (feeds.CurrentPage - 1) * 10;
  14. <table class="table table-striped">
  15. <thead>
  16. <tr>
  17. <th>Sl.No.</th>
  18. <th>Post Title</th>
  19. <th>Post Type</th>
  20. <th>Content</th>
  21. <th>Publish Date</th>
  22. <th>Author</th>
  23. </tr>
  24. </thead>
  25. <tbody>
  26. @foreach (var feed in feeds.Results)
  27. {
  28. counter++;
  29. <tr>
  30. <td>@counter</td>
  31. <td><NavLink href[email protected] target="_blank">@feed.Title</NavLink></td>
  32. <td>@feed.FeedType</td>
  33. <td>@feed.Content</td>
  34. <td>@feed.PublishDate</td>
  35. <td>@feed.Author</td>
  36. </tr>
  37. }
  38. </tbody>
  39. </table>
  40. <Pager Result=@feeds PageChanged=@PagerPageChanged />
  41. }
  42. @functions{
  43. PagedResult<Feed> feeds;
  44. string author;
  45. int counter;
  46. [Parameter]
  47. protected string Page { get; set; } = "1";
  48. protected override async Task OnParametersSetAsync()
  49. {
  50. await LoadFeeds(int.Parse(Page));
  51. }
  52. private async Task LoadFeeds(int page)
  53. {
  54. author = "";
  55. feeds = await Http.GetJsonAsync<PagedResult<Feed>>("/api/feeds/featuredarticles/" + page.ToString());
  56. }
  57. protected void PagerPageChanged(int page)
  58. {
  59. feeds = null;
  60. UriHelper.NavigateTo("/featuredarticles/" + page);
  61. }
  62. }

LatestAllPosts.cshtml

  1. @using BlazorPagination.Shared.Models
  2. @using BlazorPagination.Shared.Pagination
  3. @page "/latestallposts/{Page}"
  4. @inject HttpClient Http
  5. @inject Microsoft.AspNetCore.Blazor.Services.IUriHelper UriHelper
  6. <h4>C# Corner Latest Posts (All Types) List</h4>
  7. @if (feeds == null)
  8. {
  9. <p><em>Loading...</em></p>
  10. }
  11. else
  12. {
  13. counter = (feeds.CurrentPage - 1) * 10;
  14. <table class="table table-striped">
  15. <thead>
  16. <tr>
  17. <th>Sl.No.</th>
  18. <th>Post Title</th>
  19. <th>Post Type</th>
  20. <th>Content</th>
  21. <th>Publish Date</th>
  22. <th>Author</th>
  23. </tr>
  24. </thead>
  25. <tbody>
  26. @foreach (var feed in feeds.Results)
  27. {
  28. counter++;
  29. <tr>
  30. <td>@counter</td>
  31. <td><NavLink href[email protected] target="_blank">@feed.Title</NavLink></td>
  32. <td>@feed.FeedType</td>
  33. <td>@feed.Content</td>
  34. <td>@feed.PublishDate</td>
  35. <td>@feed.Author</td>
  36. </tr>
  37. }
  38. </tbody>
  39. </table>
  40. <Pager Result=@feeds PageChanged=@PagerPageChanged />
  41. }
  42. @functions{
  43. PagedResult<Feed> feeds;
  44. string author;
  45. int counter;
  46. [Parameter]
  47. protected string Page { get; set; } = "1";
  48. protected override async Task OnParametersSetAsync()
  49. {
  50. await LoadFeeds(int.Parse(Page));
  51. }
  52. private async Task LoadFeeds(int page)
  53. {
  54. author = "";
  55. feeds = await Http.GetJsonAsync<PagedResult<Feed>>("/api/feeds/latestallposts/" + page.ToString());
  56. }
  57. protected void PagerPageChanged(int page)
  58. {
  59. feeds = null;
  60. UriHelper.NavigateTo("/latestallposts/" + page);
  61. }
  62. }

LatestArticles.cshtml

  1. @using BlazorPagination.Shared.Models
  2. @using BlazorPagination.Shared.Pagination
  3. @page "/latestarticles/{Page}"
  4. @inject HttpClient Http
  5. @inject Microsoft.AspNetCore.Blazor.Services.IUriHelper UriHelper
  6. <h4>C# Corner Latest <b>Articles</b> List</h4>
  7. @if (feeds == null)
  8. {
  9. <p><em>Loading...</em></p>
  10. }
  11. else
  12. {
  13. counter = (feeds.CurrentPage - 1) * 10;
  14. <table class="table table-striped">
  15. <thead>
  16. <tr>
  17. <th>Sl.No.</th>
  18. <th>Article Title</th>
  19. <th>Content</th>
  20. <th>Publish Date</th>
  21. <th>Author</th>
  22. </tr>
  23. </thead>
  24. <tbody>
  25. @foreach (var feed in feeds.Results)
  26. {
  27. counter++;
  28. <tr>
  29. <td>@counter</td>
  30. <td><NavLink href[email protected] target="_blank">@feed.Title</NavLink></td>
  31. <td>@feed.Content</td>
  32. <td>@feed.PublishDate</td>
  33. <td>@feed.Author</td>
  34. </tr>
  35. }
  36. </tbody>
  37. </table>
  38. <Pager Result=@feeds PageChanged=@PagerPageChanged />
  39. }
  40. @functions{
  41. PagedResult<Feed> feeds;
  42. string author;
  43. int counter;
  44. [Parameter]
  45. protected string Page { get; set; } = "1";
  46. protected override async Task OnParametersSetAsync()
  47. {
  48. await LoadFeeds(int.Parse(Page));
  49. }
  50. private async Task LoadFeeds(int page)
  51. {
  52. author = "";
  53. feeds = await Http.GetJsonAsync<PagedResult<Feed>>("/api/feeds/latestarticles/" + page.ToString());
  54. }
  55. protected void PagerPageChanged(int page)
  56. {
  57. feeds = null;
  58. UriHelper.NavigateTo("/latestarticles/" + page);
  59. }
  60. }

LatestBlogs.cshtml

  1. @using BlazorPagination.Shared.Models
  2. @using BlazorPagination.Shared.Pagination
  3. @page "/latestblogs/{Page}"
  4. @inject HttpClient Http
  5. @inject Microsoft.AspNetCore.Blazor.Services.IUriHelper UriHelper
  6. <h4>C# Corner Latest <b>Blogs</b> List</h4>
  7. @if (feeds == null)
  8. {
  9. <p><em>Loading...</em></p>
  10. }
  11. else
  12. {
  13. counter = (feeds.CurrentPage - 1) * 10;
  14. <table class="table table-striped">
  15. <thead>
  16. <tr>
  17. <th>Sl.No.</th>
  18. <th>Blog Title</th>
  19. <th>Content</th>
  20. <th>Publish Date</th>
  21. <th>Author</th>
  22. </tr>
  23. </thead>
  24. <tbody>
  25. @foreach (var feed in feeds.Results)
  26. {
  27. counter++;
  28. <tr>
  29. <td>@counter</td>
  30. <td><NavLink href[email protected] target="_blank">@feed.Title</NavLink></td>
  31. <td>@feed.Content</td>
  32. <td>@feed.PublishDate</td>
  33. <td>@feed.Author</td>
  34. </tr>
  35. }
  36. </tbody>
  37. </table>
  38. <Pager Result=@feeds PageChanged=@PagerPageChanged />
  39. }
  40. @functions{
  41. PagedResult<Feed> feeds;
  42. string author;
  43. int counter;
  44. [Parameter]
  45. protected string Page { get; set; } = "1";
  46. protected override async Task OnParametersSetAsync()
  47. {
  48. await LoadFeeds(int.Parse(Page));
  49. }
  50. private async Task LoadFeeds(int page)
  51. {
  52. author = "";
  53. feeds = await Http.GetJsonAsync<PagedResult<Feed>>("/api/feeds/latestblogs/" + page.ToString());
  54. }
  55. protected void PagerPageChanged(int page)
  56. {
  57. feeds = null;
  58. UriHelper.NavigateTo("/latestblogs/" + page);
  59. }
  60. }

TopReadPosts.cshtml

  1. @using BlazorPagination.Shared.Models
  2. @using BlazorPagination.Shared.Pagination
  3. @page "/topreadposts/{Page}"
  4. @inject HttpClient Http
  5. @inject Microsoft.AspNetCore.Blazor.Services.IUriHelper UriHelper
  6. <h4>C# Corner Top Read Posts List</h4>
  7. @if (feeds == null)
  8. {
  9. <p><em>Loading...</em></p>
  10. }
  11. else
  12. {
  13. counter = (feeds.CurrentPage - 1) * 10;
  14. <table class="table table-striped">
  15. <thead>
  16. <tr>
  17. <th>Sl.No.</th>
  18. <th>Post Title</th>
  19. <th>Post Type</th>
  20. <th>Content</th>
  21. <th>Publish Date</th>
  22. <th>Author</th>
  23. </tr>
  24. </thead>
  25. <tbody>
  26. @foreach (var feed in feeds.Results)
  27. {
  28. counter++;
  29. <tr>
  30. <td>@counter</td>
  31. <td><NavLink href[email protected] target="_blank">@feed.Title</NavLink></td>
  32. <td>@feed.FeedType</td>
  33. <td>@feed.Content</td>
  34. <td>@feed.PublishDate</td>
  35. <td>@feed.Author</td>
  36. </tr>
  37. }
  38. </tbody>
  39. </table>
  40. <Pager Result=@feeds PageChanged=@PagerPageChanged />
  41. }
  42. @functions{
  43. PagedResult<Feed> feeds;
  44. string author;
  45. int counter;
  46. [Parameter]
  47. protected string Page { get; set; } = "1";
  48. protected override async Task OnParametersSetAsync()
  49. {
  50. await LoadFeeds(int.Parse(Page));
  51. }
  52. private async Task LoadFeeds(int page)
  53. {
  54. author = "";
  55. feeds = await Http.GetJsonAsync<PagedResult<Feed>>("/api/feeds/topreadposts/" + page.ToString());
  56. }
  57. protected void PagerPageChanged(int page)
  58. {
  59. feeds = null;
  60. UriHelper.NavigateTo("/topreadposts/" + page);
  61. }
  62. }

We can modify the Index.cshtml as well.

Index.cshtml
  1. @page "/"
  2. <h3>C# Corner RSS Feeds in Blazor with Pagination</h3>
  3. <hr />
  4. <p>
  5. We will see the RSS feeds from C# Corner site with pagination.
  6. We will see ten rows at a time in a page and we can have the previous,
  7. next, first and last buttons to navigate the data as our wish.
  8. We will provide all the posts by an author, featured articles list,
  9. latest posts (all types), latest articles, latest blogs, and top read articles.
  10. </p>

We have completed all the coding part. We can run the application now.

C# Corner RSS Feeds in Blazor with Pagination

You can click the “All Posts by an Author” link and enter author id to get all the post details for an author.

C# Corner RSS Feeds in Blazor with Pagination
I have given my own author ID. We will get the below details.
C# Corner RSS Feeds in Blazor with Pagination
You can get the pagination at the bottom of the page.
C# Corner RSS Feeds in Blazor with Pagination
If you click the last page button, you will get the last page details as shown below.
C# Corner RSS Feeds in Blazor with Pagination
We can get the featured articles list,
C# Corner RSS Feeds in Blazor with Pagination
We can get the latest posts (all types) list,
C# Corner RSS Feeds in Blazor with Pagination
We will get the latest articles list,
C# Corner RSS Feeds in Blazor with Pagination
We will get the latest blogs list,
C# Corner RSS Feeds in Blazor with Pagination
We can get the top read post details also.
C# Corner RSS Feeds in Blazor with Pagination

In this post, we have seen pagination in Blazor application. We have created a Pager component for that. We have got data from C# Corner RSS feeds. We have seen all the posts by an author, featured articles list, latest posts (all types), latest articles, latest blogs, and top read posts details.

I have got the idea of Blazor pagination from GunnarPeipman’s blog. I would like to express my sincere thanks to him.
I have deployed this application in Azure as a web app. If you want to check the functionalities discussed in this post, please check this URL We will discuss more features of Blazor in upcoming articles.