public
Created

  • Download Gist
gistfile1.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
using System;
using System.Text;
using System.Web.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
 
namespace Website
{
public class JsonNetResult : ActionResult
{
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()
{
SerializerSettings = new JsonSerializerSettings();
 
//SerializerSettings.Converters.Add(new JavaScriptDateTimeConverter());
SerializerSettings.Converters.Add(new IsoDateTimeConverter());
Formatting = Formatting.None;
}
 
public JsonNetResult(object data)
: this()
{
Data = data;
}
 
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
throw new ArgumentNullException("context");
 
var response = context.HttpContext.Response;
response.ContentType = !string.IsNullOrEmpty(ContentType) ? ContentType : "application/json";
if (ContentEncoding != null)
response.ContentEncoding = ContentEncoding;
 
if (Data == null) return;
var writer = new JsonTextWriter(response.Output) { Formatting = Formatting };
 
var serializer = JsonSerializer.Create(SerializerSettings);
serializer.Serialize(writer, Data);
writer.Flush();
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.