JsonViewEngine for ASP.NET MVC Framework

I am currently tasked with a project at work that, after building out the design documents, was going to rely on ASP.NET MVC and at the same time take advantage of the URL Rewriting that comes with it ‘out of the box’ for ‘data source urls’ for AJAX requests. We decided to use the jQuery library, and the $.getJSON() function within it to request for JSON data.

Well, if you’ve played with ASP.NET MVC, you’ve probably noticed that it doesn’t have any ‘quick and simple’ "json serialization in a view" … or, does it?

I recently saw Scott Guthrie at a local .NET User Group event, and he mentioned that the ASP.NET MVC Framework was completely "pluggable", and you could easily replace the built-in View Engine. So, I immediately searched for examples, found one … tossed it out and whipped together a really quick and simple ‘JsonViewEngine’ class.

Here’s the code:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Security.Permissions;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.Mvc;

namespace Sim.Microsite.Framework.Web.Mvc.JsonViewEngine
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    public class JsonViewEngine: IViewEngine
        #region IViewEngine Members
        public void RenderView(ViewContext viewContext)
            if (viewContext == null)
                throw new ArgumentNullException("viewContext");
            viewContext.HttpContext.Response.ContentType = "application/json";

            System.Text.StringBuilder jsonText = new System.Text.StringBuilder();
            JsonFx.Json.JsonWriter jsonWriter = new JsonFx.Json.JsonWriter(jsonText);

            viewContext.HttpContext.Response.Write( jsonText.ToString() );

As you may or may not be able to tell, I am using the JsonFx serializer for this project, but you could easily replace that with the built-in System.Web.Script.Serialization.JavaScriptSerializer or any other library of your choice.

After building this, I also decided to take it and create an XmlViewEngine that simply returns XML data, as we have some use for that as well with this project … I’ll leave you to implement that on your own, it’s basically the same code … just uses XmlSerializer instead — however, I wrote some custom code for serializing the IDictionary that the default ViewPage uses for ViewData (even though ViewData is defined as an object in the viewContext … ViewPage defines it as generic dictionary).

So … have fun with that.

2 comments for “JsonViewEngine for ASP.NET MVC Framework

  1. September 25, 2008 at 6:16 am

    Nice work! Have you got this to work with ASP.NET MVC Preview 5?

  2. September 25, 2008 at 7:16 am

    no, I have not tried any of the newer MVC frameworks, however, I will most likely be doing some ‘latest and greatest framework’ ports in the coming month… we’ll see.

Leave a Reply