0

When my json gets returned from the server, it is in CamelCase, but I need lowercase. I have seen a lot of solutions for ASP.NET Web API and Core, but nothing for ASP.NET MVC 5.

[HttpGet]
public JsonResult Method()
{
    var vms = new List<MyViewModel>()
    {
         new MyViewModel()
         {
              Name = "John Smith",
         }
    };

    return Json(new { results = vms }, JsonRequestBehavior.AllowGet);
}

I want "Names" to be lowercase.

3
  • This is camelCase this is PascalCase and this is lowercase do you really want lowercase? Commented Sep 11, 2020 at 5:42
  • Right I got them mixed up and was only thinking of single words as in my example, I want the json standard which I guess is camelCase Commented Sep 11, 2020 at 6:20
  • You can use either an action filter or an approach like that in @dom's answer to plug in a serializer like Newtonsoft.Json that is configurable. However, you'll have to write your own property name convention for Newtonsoft.Json because all lowercase is so uncommon that I've never seen any JSON provide it as a built-in option Commented Sep 11, 2020 at 6:26

1 Answer 1

3

The best solution I have for this is to override the default Json method to use Newtonsoft.Json and set it to use camelcase by default.

First thing is you need to make a base controller if you don't have one already and make your controllers inherit that.

public class BaseController : Controller {
}

Next you create a JsonResult class that will use Newtonsoft.Json :

public class JsonCamelcaseResult : JsonResult
{
    private static readonly JsonSerializerSettings _settings = new JsonSerializerSettings
    {
        ContractResolver = new CamelCasePropertyNamesContractResolver(),
        Converters = new List<JsonConverter> { new StringEnumConverter() }
    };

    public override void ExecuteResult(ControllerContext context)
    {
        HttpResponseBase response = context.HttpContext.Response;
        response.ContentType = !String.IsNullOrEmpty(this.ContentType) ? this.ContentType : "application/json";
        response.ContentEncoding = this.ContentEncoding ?? response.ContentEncoding;

        if (this.Data == null)
            return;

        response.Write(JsonConvert.SerializeObject(this.Data, _settings));
    }
}

Then in your BaseController you override the Json method :

protected new JsonResult Json(object data)
{
    return new JsonCamelcaseResult
    {
        Data = data,
        JsonRequestBehavior = JsonRequestBehavior.AllowGet
    };
}

So in the end in your original action returning JSON you just keep it the same and the properties will be camelcased (propertyName) instead of pascalcased (PropertyName) :

[HttpGet]
public JsonResult Method()
{
    var vms = new List<MyViewModel>()
    {
         new MyViewModel()
         {
              Name = "John Smith",
         }
    };

    return Json(new { results = vms });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.