Search code examples
c#asp.net-mvcrazoricalendaractionresult

Link filecontentresult from a razor view? Only getting the text dump via action/url


Using the ical.net nuget package, I wanted to put together a simple ical download link for events that show up in a custom list.

I've tried using an actionlink, and Html.Beginform in the view, but both give the same result. Just a 404, with the url/controller/action?start="ical text contents". Is there a different way I need to be calling this to get an actual file name?

[HttpPost]
    public FileContentResult DownloadiCal(DateTime start, DateTime end, string name, string location, string description)
    {
        var e = new CalendarEvent
        {
            Start = new CalDateTime(start),
            End = new CalDateTime(end),
            Location = location,
            Description = description
        };

        var calendar = new Calendar();
        calendar.Events.Add(e);

        var serializer = new CalendarSerializer();
        var serializedCalendar = serializer.SerializeToString(calendar);

        byte[] calendarBytes = System.Text.Encoding.UTF8.GetBytes(serializedCalendar);  //iCal is the calendar string

        return File(calendarBytes, "text/calendar", "event.ics");
    }

Solution

  • Learned how to chill and realized that there's an issue with routing. Resolved that and I'm #blessed with ics files and Stackoverflow knowledge.