Hvordan legge til CamelCasePropertyNamesContractResolver i Startup.cs?

stemmer
24

Her er min Configuremetode fra min Startupklasse.

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

Hvor kan jeg få tilgang til HttpConfigurationeksempel, slik at jeg kunne sette CamelCasePropertyNamesContractResolver, akkurat som jeg gjorde i WebAPI 2:

var formatterSettings = config.Formatters.JsonFormatter.SerializerSettings;
formatterSettings.Formatting = Formatting.None;
formatterSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
Publisert på 15/10/2014 klokken 22:53
kilden bruker
På andre språk...                            


2 svar

stemmer
40

Bytt ut services.AddMvc();med følgende.

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

OPPDATER

Med dagens versjon av ASP.NET, bør dette gjøre.

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

Med ASP.NET 5 beta5, som leveres med Visual Studio 2015 RTM, følgende koden fungerer

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

UPDATE 3

Med ASP.NET 5 beta7, følgende koden fungerer

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

RC2 UPDATE

MVC serialiserer nå JSON med kamel case navn som standard. Se denne kunngjøringen. https://github.com/aspnet/Announcements/issues/194

Svarte 16/10/2014 kl. 08:07
kilden bruker

stemmer
23

Konfigurasjon-funksjon ble fjernet fra services.AddMvc () i enten Beta 6 eller 7. For Beta 7, i Startup.cs, legge til følgende i ConfigureServices funksjon:

services.AddMvc().AddJsonOptions(options =>
{
    options.SerializerSettings.ContractResolver = 
        new CamelCasePropertyNamesContractResolver();
});
Svarte 16/09/2015 kl. 19:50
kilden bruker

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more