Map a static page

Map one Razor component to one route with MapStatic, and learn which conventions apply to every Bevel route.

On this page
Program.cscsharp
app.MapStatic<HomePage>("/");
app.MapStatic<SchedulePage>("/schedule");

Each call maps one Razor component to one route. On export, / becomes index.html and /schedule becomes schedule/index.html.

samples/Bevel.Conference/Program.cscsharp
app.MapStatic<SchedulePage>("/schedule");
  1. Write the page

    Create a Razor component. Use @inject to read ISiteData, your typed data.

    Pages/SchedulePage.razorrazor
    @inject ISiteData Data
    
    <h1>Schedule</h1>
  2. Map it

    Call MapStatic<TPage>(route) in Program.cs, after builder.Build().

  3. Export

    Run dotnet run -- --bevel-build. Bevel renders every mapped page into plain files.

Routes live in one place

You declare routes only in MapStatic calls. A page that declares @page "/x" is rejected. Your data has no routes either. Bevel builds links from these mappings at runtime. See Link between pages.

Conventions apply to every Bevel route

Every MapStatic* call returns a RouteHandlerBuilder. Conventions you add apply to all endpoints the call creates: the main route, the .html or index.html alias, and the first-page route of a paged mapping. WithName names the main route only.

csharp
app.MapStatic<AdminPage>("/admin/{Id}", (ISiteData d) => d.Reports)
   .RequireAuthorization("Editors");

That route stops the export, because a static file cannot check who is asking. See Authorization and exports.

Group routes under a prefix

Group prefixes are part of the route. This serves, exports and links /blog/posts/x:

csharp
app.MapGroup("/blog").MapStatic<PostPage>("/posts/{Slug}", (ISiteData d) => d.Posts);

A prefix with route parameters cannot be exported.

Status-code pages

Map a 404 page and re-execute it for unknown URLs:

csharp
app.UseStatusCodePagesWithReExecute("/404.html");
app.MapStatic<NotFoundPage>("/404.html");

The page renders and keeps the original status code.