Skip to content

Instantly share code, notes, and snippets.

@rgripper
Created April 22, 2013 11:40
Show Gist options
  • Save rgripper/5434106 to your computer and use it in GitHub Desktop.
Save rgripper/5434106 to your computer and use it in GitHub Desktop.
CamelCaseJonResult
public class CamelCaseJonResult : JsonResult
{
public override void ExecuteResult(System.Web.Mvc.ControllerContext context)
{
base.ExecuteResult(context);
}
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();
}
System.Web.HttpResponseBase response = context.HttpContext.Response;
if (!String.IsNullOrEmpty(ContentType))
{
response.ContentType = ContentType;
}
else
{
response.ContentType = "application/json";
}
if (ContentEncoding != null)
{
response.ContentEncoding = ContentEncoding;
}
if (Data != null)
{
var result = Newtonsoft.Json.JsonConvert.SerializeObject(
Data,
Formatting.Indented,
new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() });
response.Write(result);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment