Link between pages

Build URLs from your routes with ISiteLinks. Learn output styles, path bases, Links.Route, and the CS8974 gotcha.

On this page
razor
@inject ISiteLinks Links

<a href="@Links.To(post)">Read</a>
<a href="@(Links.To<AboutPage>())">About</a>
<link rel="stylesheet" href="@Links.Asset("/css/site.css")" />

Inject ISiteLinks and build URLs from your MapStatic routes. If a link points at a page that does not exist, the export fails. Broken internal links never ship.

Choose the right method

Method Use it for Example
Links.To(item) The page of one item. Links.To(post)
Links.To<TPage>(new { ... }) A page by type and route values. Links.To<PostPage>(new { Slug = "hello" })
Links.To<TPage>() A page with no tokens. Links.To<AboutPage>()
Links.Asset(path) A file in wwwroot. Links.Asset("/css/site.css")
Links.Route(route) A single-route mapping that is not a page. Links.Route("/calendar.ics")

Links.Route throws and lists the known routes when you name a route that is not mapped. It also throws during an export when the route is excluded from the export. Routes with tokens are not matched, so use To(...) for those.

samples/Bevel.Conference/Components/MainLayout.razorrazor
<nav>
  <a href="@(Links.To<SchedulePage>())">Schedule</a>
  <a href="@Links.Route("/calendar.ics")">Calendar</a>
</nav>

Fix error CS8974

Razor can misread a generic call in an attribute. This fails with CS8974:

razor
<a href="@Links.To<HomePage>()">Home</a>

Wrap the call in parentheses:

razor
<a href="@(Links.To<HomePage>())">Home</a>

The same applies to any Links.To<TPage>(...) in an attribute. Calls without a generic argument, such as @Links.To(post), do not need parentheses.

Choose an output style

BevelOptions.OutputStyle decides how routes map to files. Links follow it.

Style Route /about Link
DirectoryIndex (default) about/index.html /about
FlatHtml about.html /about.html
Program.cscsharp
builder.AddBevel(options => options.OutputStyle = BevelOutputStyle.FlatHtml);

The root and routes that already have an extension (/rss.xml) do not change. In FlatHtml mode the live app also serves the .html URLs. See BevelOptions.OutputStyle.

When you set BevelOptions.PathBase, Links.To(...), Links.Asset(...) and Links.Route(...) add it. /css/site.css becomes /repo/css/site.css. Absolute URLs are left alone.

Hand-written href="/about" in your markup is not rewritten. Use Links.To, Links.Route or Links.Asset, or turn on RewriteHtmlLinks Since 0.3 for HTML you do not control. A <base href> element does not help: it only changes how relative URLs resolve, and a root-relative href="/about" ignores it.

Since 0.3 Markdown rendered to a MarkupString has no Links calls. Turn on RewriteHtmlLinks and Bevel prefixes root-relative links for you:

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

Now <a href="/content/x/"> becomes <a href="/repo/content/x/"> in every HTML response (pages and BevelResults.Html).

  • It changes href, src, srcset, action and poster values that start with one /.
  • It leaves absolute URLs, //cdn URLs, #fragments, mailto:, relative links and values already under the path base.
  • It skips text inside script, style, textarea, comments and escaped code.
  • It is safe to run twice. Nothing gets a double prefix.
  • A route that really begins with the path base looks like an already-prefixed link and is not prefixed.

To rewrite one string, call Links.RewriteHtmlLinks(html).

Since 0.3 Set BevelOptions.SiteUrl to your public origin, then call Links.AbsoluteUrl on a URL you built with To, Route or Asset:

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

SiteUrl keeps only the scheme, host and port. A path is ignored, because the sub-path is PathBase. AbsoluteUrl throws when SiteUrl is not set.