A simple ActionResult class that serializes an object to binary JSON format, with the help the Json.NET library.

    public class BsonResult : ActionResult
        private object _data;

        public BsonResult(object data)
            this._data = data;

        public override void ExecuteResult(ControllerContext context)
            HttpContextBase httpContextBase = context.HttpContext;
            httpContextBase.Response.Buffer = true;

            httpContextBase.Response.ContentType = "application/bson";
            JsonSerializer serializer = new JsonSerializer();
            // serialize to BSON
            BsonWriter writer = new BsonWriter(httpContextBase.Response.OutputStream);
            serializer.Serialize(writer, _data);

One Response to “BSON ActionResult in ASP.NET MVC”

  1. Alexander says:

    I want to built a MVC 6 Web API and will use the BSON Format in Visual Studio 2015, but the class HttpContextBase cant be found. What must I do ???

Leave a Reply

Human Verification