owin - websockets - ¿Cómo agregar CamelCasePropertyNamesContractResolver en Startup.cs?




websockets asp net core (2)

La función Configurar se eliminó de services.AddMvc () en Beta 6 o 7. Para Beta 7, en Startup.cs, agregue lo siguiente a la función ConfigureServices:

services.AddMvc().AddJsonOptions(options =>
{
    options.SerializerSettings.ContractResolver = 
        new CamelCasePropertyNamesContractResolver();
});

Aquí está mi método de Configure de mi clase de Startup .

public void Configure(IApplicationBuilder app)
{
    // Setup configuration sources
    var configuration = new Configuration();
    configuration.AddJsonFile("config.json");
    configuration.AddEnvironmentVariables();

    // Set up application services
    app.UseServices(services =>
    {
        // Add EF services to the services container
        services.AddEntityFramework()
           .AddSqlServer();

        // Configure DbContext
        services.SetupOptions<DbContextOptions>(options =>
        {
           options.UseSqlServer(configuration.Get("Data:DefaultConnection:ConnectionString"));
        });

        // Add Identity services to the services container
        services.AddIdentitySqlServer<ApplicationDbContext, ApplicationUser>()
           .AddAuthentication();

        // Add MVC services to the services container
        services.AddMvc();
    });

    // Enable Browser Link support
    app.UseBrowserLink();

    // Add static files to the request pipeline
    app.UseStaticFiles();

    // Add cookie-based authentication to the request pipeline
    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = ClaimsIdentityOptions.DefaultAuthenticationType,
        LoginPath = new PathString("/Account/Login"),
    });

    // Add MVC to the request pipeline
    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default", 
            template: "{controller}/{action}/{id?}",
            defaults: new { controller = "Home", action = "Index" });

        routes.MapRoute(
            name: "api",
            template: "{controller}/{id?}");
    });
}

¿Dónde puedo acceder a la instancia de HttpConfiguration , para poder configurar CamelCasePropertyNamesContractResolver , tal como lo hice en WebApi 2:

var formatterSettings = config.Formatters.JsonFormatter.SerializerSettings;
formatterSettings.Formatting = Formatting.None;
formatterSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

Reemplazar services.AddMvc(); con lo siguiente.

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);
});

ACTUALIZAR

Con la versión actual de ASP.NET, esto debería hacer.

services.AddMvc().Configure<MvcOptions>(options =>
{
    options.OutputFormatters
               .Where(f => f.Instance is JsonOutputFormatter)
               .Select(f => f.Instance as JsonOutputFormatter)
               .First()
               .SerializerSettings
               .ContractResolver = new CamelCasePropertyNamesContractResolver();
});

ACTUALIZACIÓN 2

Con ASP.NET 5 beta5, que se incluye con Visual Studio 2015 RTM, el siguiente código funciona

services.AddMvc().Configure<MvcOptions>(options =>
{
    options.OutputFormatters.OfType<JsonOutputFormatter>()
           .First()
           .SerializerSettings
           .ContractResolver = new CamelCasePropertyNamesContractResolver();
});

ACTUALIZACIÓN 3

Con ASP.NET 5 beta7, funciona el siguiente código

services.AddMvc().AddJsonOptions(opt =>
{
    opt.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
});

ACTUALIZACIÓN RC2

MVC ahora serializa JSON con nombres de casos de camellos por defecto. Ver este anuncio. https://github.com/aspnet/Announcements/issues/194