如何在Startup.cs中添加CamelCasePropertyNamesContractResolver? [英] How to add CamelCasePropertyNamesContractResolver in Startup.cs?

查看:511
本文介绍了如何在Startup.cs中添加CamelCasePropertyNamesContractResolver?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的Startup类中的Configure方法.

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

在哪里可以访问HttpConfiguration实例,以便可以设置CamelCasePropertyNamesContractResolver,就像在WebApi 2中所做的一样:

Where can I access the HttpConfiguration instance, so that I could set the CamelCasePropertyNamesContractResolver, just like I did in WebApi 2:

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

推荐答案

Configure函数已从Beta 6或7中的services.AddMvc()中删除.对于Beta 7,在Startup.cs中,将以下内容添加到ConfigureServices中功能:

The Configure function was removed from services.AddMvc() in either Beta 6 or 7. For Beta 7, in Startup.cs, add the following to the ConfigureServices function:

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

这篇关于如何在Startup.cs中添加CamelCasePropertyNamesContractResolver?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆