Split a list into pages

Use MapStaticPaged to split a list over several pages, with page 1 at the root and links to the neighbours.

On this page
Program.cscsharp
app.MapStaticPaged<HomePage>("/page/{PageNumber}", (ISiteData data) => data.Posts,
    pageSize: 3, mapFirstPageAtRoot: true);

With mapFirstPageAtRoot: true, page 1 lives at / and the rest at /page/2, /page/3.

  1. Map the list

    Call MapStaticPaged<TPage> with a route that has a {PageNumber} token, the items, and a pageSize.

  2. Receive a paged result

    Your page takes a BevelPagedResult<T>. It has Items, PageNumber, TotalPages, PreviousPage and NextPage.

  3. Pass the page number to Links.To<TPage>.

    Pages/HomePage.razorrazor
    @inject ISiteLinks Links
    
    @foreach (var post in Page.Items) { <PostCard Post="post" /> }
    @if (Page.PreviousPage is { } previous)
    {
        <a rel="prev" href="@(Links.To<HomePage>(new { PageNumber = previous }))">Newer posts</a>
    }
    @if (Page.NextPage is { } next)
    {
        <a rel="next" href="@(Links.To<HomePage>(new { PageNumber = next }))">Older posts</a>
    }
    
    @code {
        [Parameter, EditorRequired] public BevelPagedResult<BlogPost> Page { get; set; } = default!;
    }

Rules for paged mappings

  • mapFirstPageAtRoot: true makes page 1 live at the root route in the export, in links and in the live app. /page/1 answers 301.
  • ISiteLinks never blocks on async item delegates. During an export, Links.To<PostPage>(new { Slug = "x" }) is checked against the routes already expanded. In live requests it is bound but not checked.