Reputation: 675
In our Web API 2 application, we configured the JSON formatting globally like this:
var jsonformatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
jsonformatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
This worked great - JSON was camelcase.., until we changed our controller methods to return a HttpReponseMessage (instead of the response model type directly) like this:
Request.CreateResponse(HttpStatusCode.OK, response);
That one change seemed to cause MVC to not use the JSON formatter. Our JSON is no longer CaemlCase.
Is this the expected/designed behavior or have I not specified the formatter correctly?
Thanks, -Mike
Upvotes: 5
Views: 3636
Reputation: 8588
Actual method that is called when you use Request.CreateResponse is this:
public static HttpResponseMessage CreateResponse<T>(
this HttpRequestMessage request, T value)
{
return request.CreateResponse<T>(HttpStatusCode.OK, value, configuration: null);
}
As you see configuration property is just being set to null.
So you can just manually take configuration from Request object and call another overload like this:
Request.CreateResponse(HttpStatusCode.OK, response, Request.GetConfiguration());
If you are interested in more detail, you can check source code of the framework. CreateResponse is defined here
Upvotes: 6