Replace services.AddMvc();
with the following.
services.AddMvc().SetupOptions<MvcOptions>(options =>
{
int position = options.OutputFormatters.FindIndex(f =>
f.Instance is JsonOutputFormatter);
var settings = new JsonSerializerSettings()
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
var formatter = new JsonOutputFormatter(settings, false);
options.OutputFormatters.Insert(position, formatter);
});
UPDATE
With the current version of ASP.NET, this should do.
services.AddMvc().Configure<MvcOptions>(options =>
{
options.OutputFormatters
.Where(f => f.Instance is JsonOutputFormatter)
.Select(f => f.Instance as JsonOutputFormatter)
.First()
.SerializerSettings
.ContractResolver = new CamelCasePropertyNamesContractResolver();
});
UPDATE 2
With ASP.NET 5 beta5, which is shipped with Visual Studio 2015 RTM, the following code works
services.AddMvc().Configure<MvcOptions>(options =>
{
options.OutputFormatters.OfType<JsonOutputFormatter>()
.First()
.SerializerSettings
.ContractResolver = new CamelCasePropertyNamesContractResolver();
});
UPDATE 3
With ASP.NET 5 beta7, the following code works
services.AddMvc().AddJsonOptions(opt =>
{
opt.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
});
RC2 UPDATE
MVC now serializes JSON with camel case names by default. See this announcement.
https://github.com/aspnet/Announcements/issues/194
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…