gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 10:47:20 25 4
gpt4 key购买 nike

这是我的 Configure方法来自我的 Startup类(class)。

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() 中删除了 Configure 函数。对于 Beta 7,在 Startup.cs 中,将以下内容添加到 ConfigureServices 函数中:

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

关于owin - 如何在 Startup.cs 中添加 CamelCasePropertyNamesContractResolver?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26393466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com