2

I have this code in my WebApiConfig.cs file and within the Register method:

var jsonFormatter=config.Formatters.JsonFormatter;
jsonFormatter.UseDataContractJsonSerializer = false;
jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

And yet despite this (and I did make sure this code was definitely being executed by using Debugger.Launch()) it is still outputting all my Json in Pascal Case.

Here is the code in the action method:

[HttpGet]
[Route("Details")]
public IHttpActionResult Details() {            
    using (var context = new Security.Context()) {              
        var user = context.Users.Current;
        if (user == null) { return Json((object)null); }
        return Json(user);
    }
}

I can't see what I'm doing wrong, is there something I am missing?

Anupheaus
  • 3,383
  • 2
  • 24
  • 30
  • Try something like var serialized = JsonConvert.SerializeObject(value, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver()}); – Stephen Walker Oct 29 '15 at 10:02
  • Thank you, that works fine, it outputs in camel case as expected, but I want to do this globally, not add this bit of code to every action. – Anupheaus Oct 29 '15 at 10:08
  • I have put a break point within the action method and saved the global configuration JsonFormatter to a local variable before the breakpoint so that I can check its settings before going into the Json call and it is set to use the CamelCasePropertyNamesContractResolver class. I just don't understand why it does not appear to be being used at all. – Anupheaus Oct 29 '15 at 10:15

2 Answers2

1

So maybe someone can explain this but I found out that using the ApiController.Json method does not appear to use any of the global formatters (I removed them all and this method still functioned and returned valid Json albeit in Pascal Case).

When I use the ApiController.Ok method, this does use the global formatters and any settings applied to them, like the CamelCasePropertyNamesContractResolver.

So my solution is to use "Ok" instead of "Json"...but why they are different I don't know...

Anupheaus
  • 3,383
  • 2
  • 24
  • 30
  • There's no `ApiController.Json` method, the one you are using is `Controller.Json` method, which is **not** part of `Web API`. – Arghya C Oct 29 '15 at 10:42
  • You mean this [ApiController.Json](https://msdn.microsoft.com/en-us/library/system.web.http.apicontroller.json(v=vs.118).aspx) doesn't exist? – Anupheaus Oct 29 '15 at 11:13
  • Hmm...this is odd! If you see the class definitions, you'll see that `Json()` is part of `System.Web.Mvc.Controller` class, not `System.Web.Http.ApiController` class! – Arghya C Oct 29 '15 at 11:51
  • Well, I'm definitely using ApiController and definitely was using Json method from the ApiController within a recently created project, but that link shows that the method exists on the ApiController class. – Anupheaus Oct 29 '15 at 12:20
  • In my opinion this is the more correct answer, `ApiController.Json` is intended to be used to return JSON in a specific format whereas with `ApiController.Ok` you are saying "return this content" and you leave it up to Web API to determine (based upon the Accept header) what the content should be serialized to (XML/JSON etc) – Lukazoid Jun 20 '16 at 10:03
1

Instead of returning IHttpActionResult, return the concrete type (User in your case). This way, you'll avoid lots of potential issues, including serialization, testability, and content negotiation as well.

Sergey Kolodiy
  • 5,829
  • 1
  • 36
  • 58
  • I like this as an answer, but at the moment, I can use Ok(user) or Unauthorized if the user is not found. If I returned User type, I guess I would have to raise an exception, is there an exception I can raise that would return a 401 status? – Anupheaus Oct 29 '15 at 11:18
  • 1
    @Anupheaus You can throw your own exception and then convert it to the appropriate status code using Web API [Exception Filters](http://www.asp.net/web-api/overview/error-handling/exception-handling). – Sergey Kolodiy Oct 29 '15 at 11:21