Tags and archives

Turn tags, categories and publish dates into group datasets, then add a page for each tag, category and month.

On this page
csharp
public sealed record BlogPost(
    string Slug,
    [GroupByDate("Archive", GroupPeriod.Month)] DateOnly Date,
    [GroupBy("Tags")] string[] Tags,
    [GroupBy("Categories")] string Category);
Program.cscsharp
app.MapStatic<TagPage>("/tags/{Slug}", (ISiteData data) => data.Posts.Tags);

You put a group attribute on a member. Bevel builds a dataset of groups from it, so data.Posts.Tags is a list you can route and loop over.

  1. Add group attributes

    Pick one attribute per member. [GroupBy] is for text, including arrays. [GroupByDate] makes a group per year, month or day. [GroupByFlag] splits on bool. The full list is in Group attributes.

    The sample blog does this on its post record:

    samples/Bevel.SampleBlog/Data/Posts/BlogPost.cscsharp
    [DataSource("Posts", "./**/*.md", OrderBy = "Date desc, Title")]
    [Process<DropDrafts>(Order = -1)]
    [Process<PostStats>]
    public sealed record BlogPost(
        [Unique(IgnoreCase = true)] string Slug,
        string Title,
        [GroupByDate("Archive", GroupPeriod.Month)]
        DateOnly Date,
        string Summary,
        [GroupBy("Tags")] string[] Tags,
        [GroupBy("Categories")] string Category,
        bool Featured,
        [Reference("Authors")] Author Author,
        string RawMarkdown,
        [Process<FirstParagraph>(nameof(RawMarkdown))]
        string Excerpt = "",
        // Optional list reference: front matter `contributors: [ada]`; absent means null (see PostPage).
        [Reference("Authors", Required = false)]
        IReadOnlyList<Author>? Contributors = null,
        int WordCount = 0,
        int ReadingMinutes = 0,
        bool Draft = false);
  2. Map a page for each group

    A group is an item like any other. Map it to a page.

    samples/Bevel.SampleBlog/Program.cscsharp
    // 1. Single static Razor pages
    // Home is a paged collection: page 1 is served at "/", the rest at /page/{PageNumber}.
    app.MapStaticPaged<HomePage>("/page/{PageNumber}", (ISiteData data) => data.Posts,
        pageSize: 3, mapFirstPageAtRoot: true);
    app.MapStatic<AboutPage>("/about");
    app.MapStatic<ReleasesPage>("/releases");
    app.MapStatic<NotFoundPage>("/404.html");
    
    // 2. One page per item. The item type comes from the delegate's return type and flows into the page's
    //    typed [Parameter]; route tokens bind to item properties.
    app.MapStatic<PostPage>("/posts/{Slug}", (ISiteData data) => data.Posts);
    app.MapStatic<TagPage>("/tags/{Slug}", (ISiteData data) => data.Posts.Tags);
    app.MapStatic<CategoryPage>("/categories/{Slug}", (ISiteData data) => data.Posts.Categories);
    app.MapStatic<ArchivePage>("/archive/{Slug}", (ISiteData data) => data.Posts.Archive);

    The slug is the route token. Tags use a slug of the text (ASP.NET Core becomes asp-net-core, C# becomes c-sharp). Months use 2026-11.

  3. Write the page

    The page parameter is the generated group type. Its Items are the posts in the group, and Text and Count are ready to show.

    Pages/TagPage.razorrazor
    @code {
      [Parameter] [EditorRequired] public BlogPostGroups.Tag Tag { get; set; } = default!;
    }

    Then loop over Tag.Items:

    razor
    <h1>#@Tag.Text</h1>
    <p>@Tag.Count @(Tag.Count == 1 ? "post" : "posts") with this tag.</p>
    @foreach (var post in Tag.Items)
    {
      <PostCard Post="post" />
    }

What Bevel generates

For each group attribute, Bevel generates a public sealed partial record in a static <Item>Groups class. Tags on BlogPost becomes BlogPostGroups.Tag. Each group has Text, Slug, Value, Count and Items.

Attribute data.Posts.… Slug example
[GroupBy("Tags")] Tags aspnetcore
[GroupBy("Categories")] Categories guides
[GroupByDate("Archive", GroupPeriod.Month)] Archive 2026-11

By default text groups sort by Count desc, Text. Date groups sort by Value desc, newest first. Change it with OrderBy = "Text" on the attribute.

Link to a group from a post

A post knows its tags as text. The group page needs the group object to build a link. The sample builds one slug-keyed index once and asks it, so a card does not scan every group:

csharp
public interface IPostTaxonomy
{
    BlogPostGroups.Archive ArchiveOf(BlogPost post);
    BlogPostGroups.Category CategoryOf(BlogPost post);
    IReadOnlyList<BlogPostGroups.Tag> TagsOf(BlogPost post);
}

Then a link is Links.To(Taxonomy.CategoryOf(Post)). The class lives in Services/, not Data/, because it uses the generated ISiteData, which the data step cannot see.