简体   繁体   English

如何将“Configuration.GetSection”从 .NET Core 5 转换为 .NET Core 6?

[英]How to convert `Configuration.GetSection` from .NET Core 5 to .NET Core 6?

I am trying to convert from .NET Core 5 to .NET Core 6.我正在尝试从 .NET Core 5 转换为 .NET Core 6。

In my .NET Core 5, Startup.cs , I have在我的 .NET Core 5, Startup.cs中,我有

public class Startup
{
        public IConfiguration Configuration { get; }

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public void ConfigureServices(IServiceCollection services)
        {
            // ...
            Configuration.GetSection("sectionName")
        }
}

In .NET Core 6, Startup.cs is removed.在 .NET Core 6 中,删除了Startup.cs And from https://docs.microsoft.com/en-us/aspnet/core/migration/50-to-60?view=aspnetcore-6.0&tabs=visual-studio , I don't see how can I read Configuration section from configuration file, appsettings.json .https://docs.microsoft.com/en-us/aspnet/core/migration/50-to-60?view=aspnetcore-6.0&tabs=visual-studio ,我看不到如何阅读Configuration部分来自配置文件appsettings.json

Can you please point me where I can find example to do that in .NET 6?你能指出我在哪里可以找到 .NET 6 中的示例吗?

In .Net 6 , Configuration has been configured in Program.cs, You just need to use:在 .Net 6 中, Configuration已经在 Program.cs 中配置好了,你只需要使用:

var builder = WebApplication.CreateBuilder(args);
//.......
builder.Services.Configure<Your model>(configuration.GetSection("sectionName"));

to Map appSettings.json object to class.将 appSettings.json 对象映射到类。

Or you can just use:或者你可以只使用:

var builder = WebApplication.CreateBuilder(args);
//.......
builder.Configuration.GetSection("sectionName")

to read the value in appsetting.json .读取appsetting.json中的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM