Render one page per item

Map a route with a token and an items delegate, and Bevel renders one page for each item in your data.

On this page
Program.cscsharp
app.MapStatic<PostPage>("/posts/{Slug}", (ISiteData data) => data.Posts);

{Slug} is filled from each item's Slug property. Bevel renders one page per item.

samples/Bevel.Conference/Program.cscsharp
// One page per item: {Slug} is filled from each session's Slug property.
app.MapStatic<SessionPage>("/sessions/{Slug}", (ISiteData data) => data.Sessions);
  1. Add a token to the route

    Put a token such as {Slug} in the route. It must match a property on the item.

  2. Return the items

    Pass a delegate that returns a collection. It can inject services and a CancellationToken, and it can be async.

  3. Receive the item in the page

    Declare a typed [Parameter]:

    razor
    @code {
        [Parameter, EditorRequired] public BlogPost Post { get; set; } = default!;
    }
samples/Bevel.Conference/Pages/SessionPage.razorrazor
<p class="crumb"><a href="@(Links.To<SchedulePage>())">← Schedule</a></p>
<p class="when">
  @Session.Start.ToString("dddd, MMMM d") ·
  @Session.Start.ToString("HH:mm") – @Session.End.ToString("HH:mm") ·
  @Session.Room
</p>
<h1>@Session.Title</h1>
<p class="by">
  with <a href="@Links.To(Session.Speaker)">@Session.Speaker.Name</a>, @Session.Speaker.Role
</p>

<Markdown Source="@Session.RawMarkdown"/>

When a page has several parameters

If more than one parameter could take the item, mark the right one with [Model]. Mark exactly one.

Format tokens and walk properties

{Date:yyyy} formats a token. {Author.Handle} walks nested properties. In Development, Bevel logs one line per mapping at startup.

Fix routing errors

Mistakes fail at the MapStatic call, not at export. Each message says what to fix. Examples:

  • items delegate returns string, which is not a collection of items.
  • the items are TagInfo, but PostPage has no [Parameter] that can receive a TagInfo.
  • route token '{Nope}' does not match any property of BlogPost. Available properties: ...
  • A per-item mapping needs a token. /posts alone fails with a message asking for /posts/{Slug}.

Link errors fail the export:

  • No mapping renders the type: add a MapStatic.
  • Two mappings render the type: use Links.To<TPage>(item).
  • No page '/posts/nope' exists: fix the value.

Slugs must be safe file names

Route segments are URL-decoded and checked. .., empty segments, separators, :, *, ? and Windows device names (con, nul, com1) are rejected. Two routes that differ only by case (/Foo and /foo) break on case-insensitive hosts. Put [Unique(IgnoreCase = true)] on slugs to catch this at build time.