<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

        时间:2023-06-08
        • <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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何覆盖 ASP.NET Core Identity 的密码策略 下一篇:ASP.NET 5 授权两个或多个策略(或组合策略)

                  相关文章

                    <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'>

                    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>