如何在ASP.NET Core中的任何类中访问Configuration? [英] How do I access Configuration in any class in ASP.NET Core?

查看:341
本文介绍了如何在ASP.NET Core中的任何类中访问Configuration?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了ASP.NET核心上的配置文档 .文档说您可以从应用程序中的任何位置访问配置.

I have gone through configuration documentation on ASP.NET core. Documentation says you can access configuration from anywhere in the application.

下面是模板创建的Startup.cs

Below is Startup.cs created by template

public class Startup
{
    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

        if (env.IsEnvironment("Development"))
        {
            // This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
            builder.AddApplicationInsightsSettings(developerMode: true);
        }

        builder.AddEnvironmentVariables();
        Configuration = builder.Build();
    }

    public IConfigurationRoot Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container
    public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddApplicationInsightsTelemetry(Configuration);

        services.AddMvc();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        app.UseApplicationInsightsRequestTelemetry();

        app.UseApplicationInsightsExceptionTelemetry();

        app.UseMvc();
    }
}

因此,在Startup.cs中,我们配置所有设置,Startup.cs也具有一个名为Configuration

So in Startup.cs we configure all the settings, Startup.cs also has a property named Configuration

我不明白如何在控制器或应用程序中的任何位置访问此配置? MS建议使用选项模式,但我只有4-5个键/值对,因此我不想使用选项模式.我只是想访问应用程序中的配置.如何在任何课程中注入它?

What I'm not able to understand how do you access this configuration in controller or anywhere in the application? MS is recommending to use options pattern but I have only 4-5 key-value pairs so I would like not to use options pattern. I just wanted to have access to Configuration in application. How do I inject it in any class?

推荐答案

更新

使用ASP.NET Core 2.0将自动在依赖项注入容器中添加应用程序的IConfiguration实例.这也可以与WebHostBuilder上的ConfigureAppConfiguration一起使用.

Update

Using ASP.NET Core 2.0 will automatically add the IConfiguration instance of your application in the dependency injection container. This also works in conjunction with ConfigureAppConfiguration on the WebHostBuilder.

例如:

public static void Main(string[] args)
{
    var host = WebHost.CreateDefaultBuilder(args)
        .ConfigureAppConfiguration(builder =>
        {
            builder.AddIniFile("foo.ini");
        })
        .UseStartup<Startup>()
        .Build();

    host.Run();
}


就像将IConfiguration实例作为ConfigureServices中的单例对象添加到服务集合一样简单:


It's just as easy as adding the IConfiguration instance to the service collection as a singleton object in ConfigureServices:

public void ConfigureServices(IServiceCollection services)
{
   services.AddSingleton<IConfiguration>(Configuration);

   // ...
}

ConfigurationStartup类中的实例.

这允许您将IConfiguration注入到任何控制器或服务中:

This allows you to inject IConfiguration in any controller or service:

public class HomeController
{
   public HomeController(IConfiguration configuration)
   {
      // Use IConfiguration instance
   }
}

这篇关于如何在ASP.NET Core中的任何类中访问Configuration?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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