Get Started!

Simple JSON

In this example the Handler returns a JSON document which is dynamically generated.

Much of the code below is boilerplate, the core of this handler is in the Handle method :

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

using Net4orce.Core;
using Net4orce.Core.Canvas;
using Net4orce.Core.Canvas.Meta;

using Newtonsoft.Json;

namespace Examples
{
  public class JSONHandler : HandlerBase, IRequiresSessionState 
  {

    public override void initialize(HttpContext context, AsyncCallback cb, object extraData)
    {
      base.initialize(context, cb, extraData);
      oOpperation = new Operation(cb, context, extraData);
    }

  }

  public class Operation : HandlerOpBase
  {
    public Operation(AsyncCallback cb, HttpContext context, Object state) 
              : base(cb, context, state) { }

    public override void Handle(object oExtraData) {

      // This is where the bespoke logic starts
      oResponse.ClearHeaders();

      oResponse.AddHeader("Content-Type", "application/json");

      oResponse.Write(

        // Generate JSON
        CanvasApp.SerializeToJSON(

          // from a dynamic / un typed object
          new {
            foo = "bar",
            hello = "World",
            obj = new {
              val = 321,
            }
          },

          // Enable indented formating
          Formatting.Indented
        )
      );

      Close();
    }
  }
}

Register the class

To register the handler use the follwoing, in your Application class :

CanvasApp.RegisterHandlerRoute(
  "json-example",
  "handlers/example/json.json",
  new JSONHandler()
);

Result / Response

The above example has been included & registered :

This is what's produced :

{
  "foo": "bar",
  "hello": "World",
  "obj": {
    "val": 321
  }
}