RSS, sitemaps and other files

Export RSS, JSON, sitemaps, robots.txt and calendar files as routes with BevelResults, and set their content types.

On this page
Program.cscsharp
app.MapStatic("/rss.xml", (ISiteData data, ISiteLinks links) =>
    BevelResults.Xml(BuildRss(data, links)));

app.MapSitemap();   // /sitemap.xml
app.MapRobots();    // /robots.txt

MapStatic(pattern, handler) takes a Minimal API handler that returns an IResult. Bevel exports the response as a file. Use it for XML, JSON, text, or any text format.

Return the right result

BevelResults has these helpers:

Helper Content type
Xml(string) application/xml; charset=utf-8
Json<T>(value) JSON
Text(string, contentType) text/plain; charset=utf-8 unless you pass another
Html(string) text/html; charset=utf-8
Razor<TComponent>(...) A rendered component

Handlers may also take HttpContext, HttpRequest and HttpResponse. The export runs each handler with its own GET request for the route's path. Anything written directly to the response body becomes the file. The status code and content type come from the response. Return nothing (or a Task) when you write directly.

Add a sitemap and robots.txt

Since 0.3 Set your public origin, then map both files:

Program.cscsharp
builder.AddBevel(o =>
{
    o.SiteUrl = "https://you.github.io";   // origin only
    o.PathBase = "/repo";                  // the sub-path, if any
});

var app = builder.Build();

app.MapSitemap(lastModified: item => (item as DocPage)?.Updated);
app.MapRobots(disallow: ["/private"]);
app.MapStatic<PrivatePage>("/private").ExcludeFromSitemap();
  • MapSitemap(route = "/sitemap.xml", lastModified = null) lists HTML routes only: extensionless and .html routes, shaped like Links (output style and path base applied) and made absolute with SiteUrl. lastModified receives the route's item (null for single pages) and returns a DateTimeOffset? for <lastmod>.
  • The sitemap skips routes with other extensions (/rss.xml, /calendar.ics), /404.html, routes with ExcludeFromStaticExport(), and routes with ExcludeFromSitemap(). An excluded route is still exported and served.
  • MapRobots(route = "/robots.txt", sitemapRoute = "/sitemap.xml", disallow = null) writes User-agent: *, one Disallow: line per path (the path base is added), and a Sitemap: line when SiteUrl is set. Pass sitemapRoute: null to omit it. No disallow entries means everything is allowed.
  • MapSitemap throws if SiteUrl is not set.

If you need a different format, write your own handler. See Read route metadata for a sitemap built by hand.

Export a calendar or other non-HTML file

The file name in the route decides the exported file:

samples/Bevel.Conference/Program.cscsharp
// A route that is not a page. The file name decides the exported file: calendar.ics.
app.MapStatic("/calendar.ics", (ISiteData data, ISiteLinks links) =>
    BevelResults.Text(CalendarFeed.Build(data.Site, data.Sessions, links), "text/calendar; charset=utf-8"));

Bevel exports the response bytes unchanged to calendar.ics. Text is UTF-8 without a byte order mark. The content type goes into .bevel-output.json and into BevelExportedFile.ContentType Since 0.3. In Production, Bevel serves the exported file with that content type.

Link to it with Links.Route, which adds the path base and fails the export if the route is missing:

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

The sample builds the calendar text itself. See CalendarFeed.Build in the Conference sample:

samples/Bevel.Conference/CalendarFeed.cscsharp
public static string Build(SiteConfig site, IEnumerable<Session> sessions, ISiteLinks links)
{
    var zone = TimeZoneInfo.FindSystemTimeZoneById(site.TimeZone);
    var lines = new List<string>
    {
        "BEGIN:VCALENDAR",
        "VERSION:2.0",
        "PRODID:-//Bevel Conf//Schedule//EN",
        "CALSCALE:GREGORIAN",
        "X-WR-CALNAME:" + Escape(site.Title)
    };

    foreach (var session in sessions)
    {
        // Sessions are written in local conference time. Calendars want UTC.
        var start = TimeZoneInfo.ConvertTimeToUtc(session.Start, zone);
        var end = TimeZoneInfo.ConvertTimeToUtc(session.End, zone);

        lines.Add("BEGIN:VEVENT");
        lines.Add($"UID:{session.Slug}@bevel-conf.example");
        // DTSTAMP says when the event was written. A fixed value keeps the file identical between builds.
        lines.Add("DTSTAMP:20261129T000000Z");
        lines.Add($"DTSTART:{start:yyyyMMdd'T'HHmmss'Z'}");
        lines.Add($"DTEND:{end:yyyyMMdd'T'HHmmss'Z'}");
        lines.Add("SUMMARY:" + Escape(session.Title));
        lines.Add("DESCRIPTION:" + Escape($"{session.Summary} Speaker: {session.Speaker.Name}."));
        lines.Add("LOCATION:" + Escape($"{session.Room}, {site.City}"));
        // links.To(...) respects PathBase. AbsoluteUrl adds the SiteUrl origin, so the URL is right wherever the site is hosted.
        lines.Add("URL:" + links.AbsoluteUrl(links.To(session)));
        lines.Add("END:VEVENT");
    }

    lines.Add("END:VCALENDAR");
    return string.Concat(lines.Select(line => Fold(line) + NewLine));
}

Search indexes

Bevel does not ship a search index. Write one as a handler (a JSON file) or run an indexer after export. See Run steps after the export.