Skip to content

Instantly share code, notes, and snippets.

@jpoehls
Created December 2, 2011 19:38
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save jpoehls/1424538 to your computer and use it in GitHub Desktop.
Save jpoehls/1424538 to your computer and use it in GitHub Desktop.
Newtonsoft Json Results for MVC
using System;
using System.Linq;
using System.Web.Mvc;
public abstract class BaseController : Controller
{
protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding)
{
return new JsonNetResult
{
ContentType = contentType,
ContentEncoding = contentEncoding,
Data = data
};
}
protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding, JsonRequestBehavior behavior)
{
return new JsonNetResult
{
ContentType = contentType,
ContentEncoding = contentEncoding,
Data = data,
JsonRequestBehavior = behavior
};
}
}
using System;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
/// <summary>
/// A <see cref="JsonResult"/> implementation that uses JSON.NET to
/// perform the serialization.
/// </summary>
public class JsonNetResult : JsonResult
{
// public JsonRequestBehavior JsonRequestBehavior { get; set; }
// public Encoding ContentEncoding { get; set; }
// public string ContentType { get; set; }
// public object Data { get; set; }
public JsonSerializerSettings SerializerSettings { get; set; }
public Formatting Formatting { get; set; }
public JsonNetResult()
{
Formatting = Formatting.None;
SerializerSettings = new JsonSerializerSettings();
JsonRequestBehavior = JsonRequestBehavior.DenyGet;
}
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
throw new ArgumentNullException("context");
if (JsonRequestBehavior == JsonRequestBehavior.DenyGet
&& String.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
{
throw new InvalidOperationException("This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet.");
}
HttpResponseBase response = context.HttpContext.Response;
response.ContentType = !string.IsNullOrEmpty(ContentType)
? ContentType
: "application/json";
if (ContentEncoding != null)
response.ContentEncoding = ContentEncoding;
if (Data != null)
{
var writer = new JsonTextWriter(response.Output) { Formatting = Formatting };
var serializer = JsonSerializer.Create(SerializerSettings);
serializer.Serialize(writer, Data);
writer.Flush();
}
}
}
@brutaldev
Copy link

Works like a charm, thanks!

@bradykelly
Copy link

Only discomfort is if I use this code, great stuff BTW, I must always cast to 'JsonResult', e.g.

[HttpGet]
[ChildActionOnly]
public JsonNetResult IndexData()
{
    var bundles = Db.PointsBundles;
    var model = new PointsBundleBundleIndexViewModel();
    model.MapItems(bundles);
    return (JsonNetResult)Json(model, null, null, JsonRequestBehavior.DenyGet);
}

@dotnetchris
Copy link

@bradykelly better solution, just make your method public ActionResult Index() no reason to need to return JsonNetResult specifically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment