如何将.NET Core 5转换为.NET CORE 6转换`configuration.getSection`?

发布于 2025-02-03 21:24:08 字数 748 浏览 5 评论 0原文

我正在尝试从.NET Core 5转换为.NET Core 6。

在我的.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")
        }
}

.net core 6,startup.cs 已删除。从 https://learn.microsoft.com/en-us/aspnet/core/migration/50-to-migration/50-to-to-migration/50?请参阅如何从配置文件,appsettings.json中读取配置

您能在.NET 6中找到我可以找到示例的地方吗?

I am trying to convert from .NET Core 5 to .NET Core 6.

In my .NET Core 5, Startup.cs, I have

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. And from https://learn.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.

Can you please point me where I can find example to do that in .NET 6?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倚栏听风 2025-02-10 21:24:08

已在程序中配置。

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

在.NET 6,配置

或者,您可以使用:

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

读取AppSetting.json中的值。

In .Net 6 , Configuration has been configured in Program.cs, You just need to use:

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

to Map appSettings.json object to class.

Or you can just use:

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

to read the value in appsetting.json.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文