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

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

      <tfoot id='GtdNs'></tfoot>

      1. <legend id='GtdNs'><style id='GtdNs'><dir id='GtdNs'><q id='GtdNs'></q></dir></style></legend>
          <bdo id='GtdNs'></bdo><ul id='GtdNs'></ul>

        在 Asp.net-core 中从类库访问 appsetting.json

        Access from class library to appsetting.json in Asp.net-core(在 Asp.net-core 中从类库访问 appsetting.json)
        • <tfoot id='gkUCA'></tfoot>

                <bdo id='gkUCA'></bdo><ul id='gkUCA'></ul>

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

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

                    <tbody id='gkUCA'></tbody>

                  本文介绍了在 Asp.net-core 中从类库访问 appsetting.json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试从类库访问 appsetting.json 文件.到目前为止,我找到的解决方案是从 Microsoft.Extensions.Configuration 创建一个实现接口 IConfiguration 的配置类,并将 json 文件添加到类并从中读取.

                  I am trying to access appsetting.json file from a class library. So far the solution that I found is to create a configuration class implementing interface IConfiguration from Microsoft.Extensions.Configuration and add the json file to class and read from the same.

                  var configuration = new Configuration();
                  configuration.AddJsonFile("appsetting.json");
                  var connectionString= configuration.Get("connectionString");
                  

                  这似乎是一个不好的选择,因为我们每次必须访问 appsetting 配置时都必须添加 json 文件.我们在 ASP.NET 中没有像 ConfigurationManager 这样的替代方案吗?

                  This seems to be bad option as we have to add the json file each time we have to access the appsetting configuration. Dont we have any alternative like ConfigurationManager in ASP.NET.

                  推荐答案

                  我假设您想从 Web 应用程序访问 appsettings.json 文件,因为类库没有 appsettings.json 默认.

                  I'm assuming you want to access the appsettings.json file from the web application since class libraries don't have an appsettings.json by default.

                  我创建了一个模型类,它的属性与 appsettings.json 中某个部分的设置相匹配.

                  I create a model class that has properties that match the settings in a section in appsettings.json.

                  appsettings.json 中的部分

                  "ApplicationSettings": {
                      "SmtpHost": "mydomain.smtp.com",
                      "EmailRecipients": "me@mydomain.com;other@mydomain.com"
                  }   
                  

                  匹配模型类

                  namespace MyApp.Models
                  {
                      public class AppSettingsModel
                      {
                          public string SmtpHost { get; set; }
                          public string EmailRecipients { get; set; }
                      }
                  }
                  

                  然后填充该模型类并将其添加到 DI 容器中的 IOptions 集合中(这是在 Startup 类的 Configure() 方法中完成的).

                  Then populate that model class and add it to the IOptions collection in the DI container (this is done in the Configure() method of the Startup class).

                  services.Configure<AppSettingsModel>(Configuration.GetSection("ApplicationSettings"));
                  
                  // Other configuration stuff
                  
                  services.AddOptions();
                  

                  然后,您可以从框架调用的任何方法访问该类,方法是将其添加为构造函数中的参数.框架负责查找类并将其提供给构造函数.

                  Then you can access that class from any method that the framework calls by adding it as a parameter in the constructor. The framework handles finding and providing the class to the constructor.

                  public class MyController: Controller
                  {
                      private IOptions<AppSettingsModel> settings;
                  
                      public MyController(IOptions<AppSettingsModel> settings)
                      {
                          this.settings = settings;
                      }
                  }
                  

                  然后当类库中的方法需要设置时,我要么单独传递设置,要么传递整个对象.

                  Then when a method in a class library needs the settings, I either pass the settings individually or pass the entire object.

                  这篇关于在 Asp.net-core 中从类库访问 appsetting.json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                  Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                  How to store delegates in a List(如何将代表存储在列表中)
                  How delegates work (in the background)?(代表如何工作(在后台)?)
                  C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                  Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)
                    <i id='aJvFD'><tr id='aJvFD'><dt id='aJvFD'><q id='aJvFD'><span id='aJvFD'><b id='aJvFD'><form id='aJvFD'><ins id='aJvFD'></ins><ul id='aJvFD'></ul><sub id='aJvFD'></sub></form><legend id='aJvFD'></legend><bdo id='aJvFD'><pre id='aJvFD'><center id='aJvFD'></center></pre></bdo></b><th id='aJvFD'></th></span></q></dt></tr></i><div id='aJvFD'><tfoot id='aJvFD'></tfoot><dl id='aJvFD'><fieldset id='aJvFD'></fieldset></dl></div>

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

                        <tbody id='aJvFD'></tbody>
                      1. <legend id='aJvFD'><style id='aJvFD'><dir id='aJvFD'><q id='aJvFD'></q></dir></style></legend>
                        <tfoot id='aJvFD'></tfoot>
                          <bdo id='aJvFD'></bdo><ul id='aJvFD'></ul>