Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I would also recommend installing json.NET but the rest is a lot easier. Below is an extension method I am using in my current application to provide better reuse, feel free to adapt it to your needs, but it should do what you need right out of the box.</p> <pre><code>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 { //http://odetocode.com/blogs/scott/archive/2013/03/25/asp-net-webapi-tip-3-camelcasing-json.aspx #if DEBUG Formatting = Formatting.Indented, //Makes the outputted Json easier reading by a human, only needed in debug #endif ContractResolver = new CamelCasePropertyNamesContractResolver() //Makes the default for properties outputted by Json to use camelCaps }; } public override void ExecuteResult(ControllerContext context) { if (context == null) throw new ArgumentNullException("context"); HttpResponseBase response = context.HttpContext.Response; response.ContentType = !string.IsNullOrEmpty(ContentType) ? ContentType : "application/json"; if (ContentEncoding != null) response.ContentEncoding = ContentEncoding; if (Data != null) { JsonTextWriter writer = new JsonTextWriter(response.Output) {Formatting = Formatting}; JsonSerializer serializer = JsonSerializer.Create(SerializerSettings); serializer.Serialize(writer, Data); writer.Flush(); } } } public static class JsonNetExtenionMethods { public static ActionResult JsonNet(this Controller controller, object data) { return new JsonNetResult() {Data = data}; } public static ActionResult JsonNet(this Controller controller, object data, string contentType) { return new JsonNetResult() { Data = data, ContentType = contentType }; } public static ActionResult JsonNet(this Controller controller, object data, Formatting formatting) { return new JsonNetResult() {Data = data, Formatting = formatting}; } } </code></pre> <p>Here's an example of using it.</p> <pre><code>public JsonNetResult Layar(string countryCode, string timestamp, string userId, string developerId, string layarName, double radius, double lat, double lon, double accuracy) { LayarModel model = new LayarModel(lat, lon, radius); return this.JsonNet(model); } </code></pre> <p>The part to note that solves your problem specifically is when the <code>ContractResolver</code> on the <code>JsonSerializerSettings</code> is set to use <code>new CamelCasePropertyNamesContractResolver()</code></p> <p>This way you never have to set custom naming again.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload