• <tfoot id='dj6SV'></tfoot>
    • <bdo id='dj6SV'></bdo><ul id='dj6SV'></ul>

    <small id='dj6SV'></small><noframes id='dj6SV'>

        <legend id='dj6SV'><style id='dj6SV'><dir id='dj6SV'><q id='dj6SV'></q></dir></style></legend>

        <i id='dj6SV'><tr id='dj6SV'><dt id='dj6SV'><q id='dj6SV'><span id='dj6SV'><b id='dj6SV'><form id='dj6SV'><ins id='dj6SV'></ins><ul id='dj6SV'></ul><sub id='dj6SV'></sub></form><legend id='dj6SV'></legend><bdo id='dj6SV'><pre id='dj6SV'><center id='dj6SV'></center></pre></bdo></b><th id='dj6SV'></th></span></q></dt></tr></i><div id='dj6SV'><tfoot id='dj6SV'></tfoot><dl id='dj6SV'><fieldset id='dj6SV'></fieldset></dl></div>

        如何访问 ASP.NET Core 中任何类的配置?

        How do I access Configuration in any class in ASP.NET Core?(如何访问 ASP.NET Core 中任何类的配置?)
        <i id='G6MZ6'><tr id='G6MZ6'><dt id='G6MZ6'><q id='G6MZ6'><span id='G6MZ6'><b id='G6MZ6'><form id='G6MZ6'><ins id='G6MZ6'></ins><ul id='G6MZ6'></ul><sub id='G6MZ6'></sub></form><legend id='G6MZ6'></legend><bdo id='G6MZ6'><pre id='G6MZ6'><center id='G6MZ6'></center></pre></bdo></b><th id='G6MZ6'></th></span></q></dt></tr></i><div id='G6MZ6'><tfoot id='G6MZ6'></tfoot><dl id='G6MZ6'><fieldset id='G6MZ6'></fieldset></dl></div>
        <tfoot id='G6MZ6'></tfoot>

                <tbody id='G6MZ6'></tbody>

              <legend id='G6MZ6'><style id='G6MZ6'><dir id='G6MZ6'><q id='G6MZ6'></q></dir></style></legend>

              <small id='G6MZ6'></small><noframes id='G6MZ6'>

              • <bdo id='G6MZ6'></bdo><ul id='G6MZ6'></ul>
                • 本文介绍了如何访问 ASP.NET Core 中任何类的配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我已经阅读了关于 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 建议使用 options pattern 但我只有 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 中任何类的配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to keep the Text of a Read only TextBox after PostBack()?(PostBack()之后如何保留只读文本框的文本?)
                  Winforms Textbox - Using Ctrl-Backspace to Delete Whole Word(Winforms 文本框 - 使用 Ctrl-Backspace 删除整个单词)
                  C# - Add button click events using code(C# - 使用代码添加按钮单击事件)
                  Multi-color TextBox C#(多色文本框 C#)
                  How can i set the caret position to a specific index in passwordbox in WPF(如何将插入符号位置设置为 WPF 密码框中的特定索引)
                  C# Numeric Only TextBox Control(C# 纯数字文本框控件)

                  <small id='wmcNS'></small><noframes id='wmcNS'>

                    <bdo id='wmcNS'></bdo><ul id='wmcNS'></ul>
                    <legend id='wmcNS'><style id='wmcNS'><dir id='wmcNS'><q id='wmcNS'></q></dir></style></legend>

                    • <tfoot id='wmcNS'></tfoot>
                          <tbody id='wmcNS'></tbody>

                            <i id='wmcNS'><tr id='wmcNS'><dt id='wmcNS'><q id='wmcNS'><span id='wmcNS'><b id='wmcNS'><form id='wmcNS'><ins id='wmcNS'></ins><ul id='wmcNS'></ul><sub id='wmcNS'></sub></form><legend id='wmcNS'></legend><bdo id='wmcNS'><pre id='wmcNS'><center id='wmcNS'></center></pre></bdo></b><th id='wmcNS'></th></span></q></dt></tr></i><div id='wmcNS'><tfoot id='wmcNS'></tfoot><dl id='wmcNS'><fieldset id='wmcNS'></fieldset></dl></div>