6. Calendar feed

Map a route that is not a page and write a calendar.ics file attendees can add to their calendar app.

On this page
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"));

Not every file is a web page. Calendar apps read .ics files. Bevel can export those too: MapStatic takes a handler that returns text, XML or JSON, and the file name in the route names the exported file.

  1. Map the route

    Add the route above to Program.cs. BevelResults.Text returns plain text with the content type you choose. The handler asks for ISiteData and ISiteLinks, and Bevel supplies both.

  2. Write the feed

    An iCalendar file is plain text with a few strict rules. Create CalendarFeed.cs in the project root. This is the whole file, so you can copy it as it is:

    samples/Bevel.Conference/CalendarFeed.cscsharp
    using System.Text;
    using Bevel.Conference.Data;
    
    namespace Bevel.Conference;
    
    /// <summary>Builds an iCalendar (.ics) feed. The format is plain text with a few strict rules.</summary>
    public static class CalendarFeed
    {
        // iCalendar lines end in CRLF, not just LF.
        private const string NewLine = "\r\n";
    
        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));
        }
    
        /// <summary>Escapes the characters that have a meaning inside an iCalendar text value.</summary>
        private static string Escape(string text) => text
            .Replace("\\", "\\\\")
            .Replace(";", "\\;")
            .Replace(",", "\\,")
            .Replace("\r\n", "\\n")
            .Replace("\n", "\\n");
    
        /// <summary>
        ///     Lines may be at most 75 bytes. A longer line continues on the next line, which starts with one space.
        ///     Counting bytes (not characters) keeps multi-byte letters like "ã" whole.
        /// </summary>
        private static string Fold(string line)
        {
            var result = new StringBuilder();
            var bytesInLine = 0;
    
            foreach (var rune in line.EnumerateRunes())
            {
                var size = rune.Utf8SequenceLength;
                if (bytesInLine + size > 75)
                {
                    result.Append(NewLine).Append(' ');
                    bytesInLine = 1; // the leading space counts
                }
    
                result.Append(rune.ToString());
                bytesInLine += size;
            }
    
            return result.ToString();
        }
    }

    Here is what the parts do:

    • Build writes one VEVENT per session. Session times are in local conference time, so it converts them to UTC with the TimeZone setting from site.yaml.
    • DTSTAMP is a fixed value, so the file is identical between builds.
    • links.AbsoluteUrl(links.To(session)) adds the site origin to a link that already has your path base, so the URL is right wherever you host. It needs options.SiteUrl, which the sample sets in Program.cs from the BEVEL_SITE_URL environment variable. Step 7 shows it.
    • Escape handles the characters that mean something in iCalendar: backslash, semicolon, comma and line breaks.
    • Fold splits lines longer than 75 bytes. It counts bytes, not characters, so a letter like "ã" stays whole. Both Fold and Build use the NewLine constant, because iCalendar lines end in \r\n.
  3. Add the using

    The route in Program.cs calls CalendarFeed. Your Program.cs needs using Bevel.Conference; at the top, next to the other using lines. Program.cs should now start like this:

    Program.cscsharp
    using Bevel.Conference;
    using Bevel.Conference.Data;
    using Bevel.Conference.Pages;
  4. Add a link in Components/MainLayout.razor, inside the <nav>. Links.Route links to a route that has no page behind it:

    razor
    <a href="@Links.Route("/calendar.ics")">Calendar</a>

    Add two buttons to the hero section of Pages/HomePage.razor, under the tagline:

    razor
    <p>
      <a class="button" href="@(Links.To<SchedulePage>())">See the schedule</a>
      <a class="button ghost" href="@Links.Route("/calendar.ics")">Add to your calendar</a>
    </p>

Run it

Open /calendar.ics. You should see a text file that starts with BEGIN:VCALENDAR and has one BEGIN:VEVENT per session.

text
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Bevel Conf//Schedule//EN
CALSCALE:GREGORIAN
X-WR-CALNAME:Bevel Conf 2027
BEGIN:VEVENT
UID:keynote-static-first@bevel-conf.example

Your browser may download it instead of showing it. That is fine. Open the file and your calendar app offers to import eight events.

The finished home page with the Add to your calendar button next to See the schedule.
Step 6: the home page with a calendar button. The feed is a file, so it has no screenshot of its own.

Next: 7. Ship to GitHub Pages.