Próbuję zwrócić wielbłąda cased JSON z kontrolera ASP.Net Web API 2. Stworzyłem nową aplikację internetową z tylko bitami ASP.Net MVC i Web API. I porwali ValuesController tak:Powrót camelCased JSON z Web API
public class ValuesController : ApiController
{
public class Thing
{
public int Id { get; set; }
public string FirstName { get; set; }
public string ISBN { get; set; }
public DateTime ReleaseDate { get; set; }
public string[] Tags { get; set; }
}
// GET api/values
public IHttpActionResult Get()
{
var thing = new Thing
{
Id = 123,
FirstName = "Brian",
ISBN = "ABC213",
ReleaseDate = DateTime.Now,
Tags = new string[] { "A", "B", "C", "D"}
};
return Json(thing);
}
}
Running to w IE, otrzymuję następujące wyniki:
{"Id":123,"FirstName":"Brian","ISBN":"ABC213","ReleaseDate":"2014-10-20T16:26:33.6810554-04:00","Tags":["A","B","C","D"]}
Obserwuje K. Scott Allen's post na temat, dodałem następujące metody rejestracji w WebApiConfig. cs file:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
var formatters = GlobalConfiguration.Configuration.Formatters;
var jsonFormatter = formatters.JsonFormatter;
var settings = jsonFormatter.SerializerSettings;
settings.Formatting = Formatting.Indented;
settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
Jednak wciąż otrzymuję to samo, kapitalizuje moje wyniki. Czy jest coś, czego mi brakuje? Próbowałem już kilku innych podejść, ale nic nie działa.
Czy jest to tak oczywista odpowiedź, że ludzie myślą, że powinienem sam to sobie wyobrazić? Nie mam jeszcze skubać. Zacząłem od nowego projektu i nadal nie byłem w stanie tego zrobić. – beaudetious
Nadal uważam, że odpowiedź z innego postu jest najbardziej przyzwoita: http://stackoverflow.com/a/22130487/1915401 – Xinan