Skip to content

Instantly share code, notes, and snippets.

@wcadap
Created January 1, 2015 09:19
Show Gist options
  • Save wcadap/e4c11699f6281100711e to your computer and use it in GitHub Desktop.
Save wcadap/e4c11699f6281100711e to your computer and use it in GitHub Desktop.
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Net;
using System.Web.Mvc;
namespace Contacts.Helpers
{
public class JsonController : Controller
{
//
// GET: /Json/
public new ActionResult Json(object data, JsonRequestBehavior behavior)
{
var jsonSerializerSetting = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
if (Request.RequestType == WebRequestMethods.Http.Get && behavior == JsonRequestBehavior.DenyGet)
{
throw new InvalidOperationException("GET is not permitted for this request.");
}
var jsonResult = new ContentResult
{
Content = JsonConvert.SerializeObject(data, jsonSerializerSetting),
ContentType = "application/json"
};
return jsonResult;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment