Created
December 2, 2011 19:38
-
-
Save jpoehls/1424538 to your computer and use it in GitHub Desktop.
Newtonsoft Json Results for MVC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
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);
}
@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
Works like a charm, thanks!