owin – 如何在Startup.cs中添加CamelCasePropertyNamesContractResolver?

这是我的启动类中的配置方法
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中所做的那样:

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

解决方法

配置功能已从Beta 6或7中的services.AddMvc()中删除。对于Beta 7,在Startup.cs中,将以下内容添加到ConfigureServices函数中:
services.AddMvc().AddJsonoptions(options =>
{
    options.SerializerSettings.ContractResolver = 
        new CamelCasePropertyNamesContractResolver();
});

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....