Wednesday, January 30, 2013

How to generate and download iCalendar file in ASP.NET MVC?

public ActionResult DownloadiCalendar()
        {
            MemoryStream output = new MemoryStream();
            StreamWriter writer = new StreamWriter(output, System.Text.Encoding.UTF8);
            writer.WriteLine(GetCalendarString());
            writer.Flush();
            output.Seek(0, SeekOrigin.Begin);
            return File(output, "text/calendar", "appointment.ics");
        }

        private string GetCalendarString()
        {
            string DateFormat = "yyyyMMddTHHmmssZ";
            DateTime startDate = DateTime.Now.AddDays(5);
            DateTime endDate = startDate.AddMinutes(35);
            string organizer = "foo@bar.com";
            string location = "My House";
            string summary = "My Event";
            string description = "Please come to\\nMy House";


            string calendar = "BEGIN:VCALENDAR";
            calendar = calendar + "\nVERSION:2.0";
            calendar = calendar + "\nMETHOD:PUBLISH";
            calendar = calendar + "\nBEGIN:VEVENT";
            calendar = calendar + "\nORGANIZER:MAILTO:" + organizer;
            calendar = calendar + "\nDTSTART:" + startDate.ToUniversalTime().ToString(DateFormat);
            calendar = calendar + "\nDTEND:" + endDate.ToUniversalTime().ToString(DateFormat);
            calendar = calendar + "\nLOCATION:" + location;
            calendar = calendar + "\nUID:" + DateTime.Now.ToUniversalTime().ToString(DateFormat) + "@mysite.com";
            calendar = calendar + "\nDTSTAMP:" + DateTime.Now.ToUniversalTime().ToString(DateFormat);
            calendar = calendar + "\nSUMMARY:" + summary;
            calendar = calendar + "\nDESCRIPTION:" + description;
            calendar = calendar + "\nPRIORITY:5";
            calendar = calendar + "\nCLASS:PUBLIC";
            calendar = calendar + "\nEND:VEVENT";
            calendar = calendar + "\nEND:VCALENDAR";

            return calendar;
        }

How run a ASP.NET project without Visual Studio and IIS configuration?

By ASP.NET development server:

"C:\Program Files (x86)\Common Files\microsoft shared\DevServer\10.0\webdev.webserver40.exe" /path:"C:\ProjectFolder" /vpath:/