Host under a sub-path

Set PathBase so links and assets work when your site lives at /repo instead of the domain root, live and in an export.

On this page
Program.cscsharp
builder.AddBevel(options => options.PathBase = "/repo");

Set BevelOptions.PathBase when the site lives under a sub-path, such as https://you.github.io/repo/. Bevel normalises it to /segment[/segment] with no trailing slash. "/" and empty mean none. ?, #, { and } throw.

What changes, and what does not

  • Links change. Links.To(...) returns /repo/posts/x/. Links.Asset("/css/site.css") returns /repo/css/site.css. Links.Route(...) also gets the prefix. Absolute URLs are not touched.
  • Export layout does not change. Files are written at unprefixed paths: posts/x/index.html, not repo/posts/x/index.html. You upload the output folder so the host serves it at /repo/.
  • Rendering sees it. Pages render with HttpRequest.PathBase set to the option, so NavigationManager and <base href> agree.
  • The option wins over the request's PathBase, so nothing is prefixed twice. If the option is empty, live requests use the request's PathBase.

Set it from the environment

Pass the path base from the environment, so the same code works locally and in CI:

samples/Bevel.Conference/Program.cscsharp
// Hosted under a sub-path (GitHub Pages project sites)? Set it here and every link and asset follows.
// Pass it from the environment so the same code works locally and in CI: BEVEL_PATH_BASE=/bevel-conf
builder.AddBevel(options =>
{
    options.PathBase = Environment.GetEnvironmentVariable("BEVEL_PATH_BASE") ?? "";
    // The origin of the published site. The calendar feed needs it for absolute session URLs.
    options.SiteUrl = Environment.GetEnvironmentVariable("BEVEL_SITE_URL") ?? "https://example.com";
});

Then export with the variable set:

bash
BEVEL_PATH_BASE=/bevel-conf dotnet run -c Release -- --bevel-build dist

This documentation site does it by environment: it uses "" under dotnet watch and /bevel in the export that GitHub Pages hosts. See Program.cs. You are reading the live example.

Serve live under a sub-path

Bevel never calls UsePathBase. When your ASP.NET Core app serves the site under a sub-path, call it yourself, before the Bevel endpoints:

Program.cscsharp
var app = builder.Build();
app.UsePathBase("/repo");
app.UseStaticFiles();
app.MapStatic<HomePage>("/");

Routing and the exported-file server then see unprefixed paths.

Hand-written href="/about" in your markup is not rewritten. Pick one:

  • Use Links: href="@(Links.To<AboutPage>())".
  • Turn on RewriteHtmlLinks Since 0.3 for HTML you cannot change, such as Markdown rendered to a MarkupString.

Do not reach for <base href>. It only affects relative URLs such as href="about". A root-relative href="/about" ignores it, and it also changes how fragment links (href="#top") resolve, so they point back to the base page.

csharp
builder.AddBevel(options =>
{
    options.PathBase = "/repo";
    options.RewriteHtmlLinks = true;
});

It prefixes root-relative href, src, srcset, action and poster values in every HTML response. /content/x/ becomes /repo/content/x/. Values already under /repo, absolute URLs, //cdn URLs, #fragments and mailto: stay as they are. Call Links.RewriteHtmlLinks(html) to rewrite one string. See Rewrite links in HTML you cannot change.

Combine with SiteUrl

Since 0.3 SiteUrl is the public origin only, and the sub-path is PathBase. They work together:

csharp
builder.AddBevel(o =>
{
    o.SiteUrl = "https://you.github.io";   // scheme, host, port
    o.PathBase = "/repo";                  // sub-path
});
// Links.AbsoluteUrl(Links.To(page)) -> https://you.github.io/repo/guide/x

If you pass SiteUrl = "https://you.github.io/repo/", Bevel ignores the /repo part. Set PathBase too. Sitemap URLs are built from both.