Create FullCalendar JSON Feed in C#

I've received a couple of questions about past blog posts concerning FullCalendar, primarily How did you generate your JSON feed for FullCalendar?

The previous blog posts were FullCalendar Event Description with Modal Pop-up and FullCalendar Event Filtering.

Here is how to create an ASHX Handler in C# to generate a JSON feed for your FullCalendar.

1. Create your Calendar class

This is what we will turn into JSON. This is a stripped down version for simplicity.

using System.Runtime.Serialization;

namespace Business
{
    [DataContract(Name="CalendarEvent")]
    public class CalendarEvent
    {
        [DataMember]
        private string title;
        [DataMember]
        private string description;
        [DataMember]
        private string start;
        [DataMember]
        private string end;
        [DataMember]
        private string allday;
        [DataMember]
        private string url;
        [DataMember]
        private string color;
        [DataMember]
        private string borderColor;
        [DataMember]
        private string textColor;


        public CalendarEvent()
        {
        }

        public CalendarEvent(string t, string d, string s, string e, string a, string u, string col = null, string bcol = null, string tcol = null)
        {
            title = t;
            description = d;
            start = s;
            end = e;
            allday = a;
            url = u;
            color = col;
            borderColor = bcol;
            textColor = tcol;
        }
    }
}

Note: You'll likely need to add a Reference to System.Runtime.Serialization. (Right click References -> Add Reference)

Add reference to System.Runtime.Serialization

2. Create your ASHX handler.

Right-click your project -> Add New Item -> Generic Handler

Add Handler

using Business;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Web;

namespace MSDev
{
    public class calendarjson : IHttpHandler
    {
        private List _cal = new List();

        public void ProcessRequest(HttpContext context)
        {
            //Here is where you populate List _cal with your source events

            //Instead, I'll load fake data for Events 1
            if (context.Request.QueryString["e1"] != "false")
            {
                for (int x = 1; x < 10; x++)
                {

                    int addDay = x % 2 == 0 ? x : x + 1;
                    DateTime startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, addDay);
                    DateTime endDate = x % 4 == 0 ? new DateTime(DateTime.Now.Year, DateTime.Now.Month, addDay + 2) : new DateTime(DateTime.Now.Year, DateTime.Now.Month, addDay);
                    _cal.Add(new CalendarEvent(String.Format("Event1 {0}", x), "

This is just a test event for source 1.

Nothing to see really.

", startDate.ToString("yyyy-MM-dd HH:mm"), endDate.ToString("yyyy-MM-dd HH:mm"), "false", "http://mikesmithdev.com", "#9fc6e7", "#1587bd", "#000000")); } } //And then I'll load fake data for Events 2 if (context.Request.QueryString["e2"] != "false") { for (int x = 7; x < 23; x += 2) { int addDay = x % 3 == 0 ? x : x + 2; _cal.Add(new CalendarEvent(String.Format("Event2 {0}", x), "This is just a test event. Nothing to see really.", DateTime.Now.AddDays(addDay).ToString("yyyy-MM-dd HH:mm"), null, "false", "http://mikesmithdev.com", "#7c6995", "#5b447a", "#ffffff")); } } //This is the important part! MemoryStream stream = new MemoryStream(); DataContractJsonSerializer s = new DataContractJsonSerializer(typeof(List)); s.WriteObject(stream, _cal); stream.Position = 0; StreamReader sr = new StreamReader(stream); context.Response.ContentType = "application/json"; context.Response.Write(sr.ReadToEnd()); } public bool IsReusable { get { return false; } } } }

And that is it. Now just point your FullCalendar to your ashx file.

Happy Coding.