简体   繁体   English

访问库项目中的配置object(appsettings.json)

[英]Access configuration object (appsettings.json) in library project

I started a .net core web api project.我启动了一个 .net 内核 web api 项目。 I have one solution with the main project and many other project like DAL, Common etc.我有一个主要项目和许多其他项目(如 DAL、Common 等)的解决方案。

I want to create a class in the Common project that will share configuration settings(appsettings.json) between my different library project.我想在 Common 项目中创建一个 class ,它将在我的不同库项目之间共享配置设置(appsettings.json)。 So, How can I acces Iconfiguration from that class?那么,如何从 class 访问 Iconfiguration?

Is this a good way to do this?这是一个好方法吗? Or should I create a class into the main project to get configuration data from other project?或者我应该在主项目中创建一个 class 以从其他项目获取配置数据?

One way to do it would be to create a Configuration Model based on your section/subsection from your appsettings.json file, add the configuration section in your Startup.cs class, then you can use the built-in dependency injection to inject the configuration anywhere you would need it.一种方法是根据 appsettings.json 文件中的部分/子部分创建配置 Model,在 Startup.cs class 中添加配置部分,然后您可以将配置注入到内置的依赖项注入中任何你需要它的地方。

For Example例如

appsettings.json: appsettings.json:

{
  "MyConfiguration" : {
     "Configuration1" : "1",
   }
}

Your configuration model:您的配置 model:

public class MyConfiguration 
{
   public int Configuration1 { get; set; }
}

Then in the StartupClass in the ConfigureServices method you have to add然后在 ConfigureServices 方法的 StartupClass 中,您必须添加

services.Configure<MyConfiguration>(configuration.GetSection("MyConfiguration"));
services.AddOptions();

And when you want to use that Configuration somewhere in the code, you would just inject it in the constructor like so:当您想在代码中的某处使用该配置时,您只需将其注入构造函数中,如下所示:

public class SomeClass 
{
  private MyConfiguration MyConfiguration { get; }

  public SomeClass(IOptions<MyConfiguration> myConfigurationOptions)
  {
    MyConfiguration = myConfigurationOptions.Value;
  }

//... more code
}

Then you can access it然后就可以访问了

MyConfiguration.Configuration1

You can add the file as a link in yout project.您可以将该文件as a link添加到您的项目中。

  1. Right click your project, then select "Add" ->"Existing Item..."右键单击您的项目,然后 select "Add" ->"Existing Item..."
  2. Choose the file that you want to add to the solution选择要添加到解决方案的文件
  3. Instead of hitting Enter or clicking the Add button, you need to click the down-arrow icon at the right edge of the Add button , and select " Add As Link ".您需要单击“添加”按钮右边缘的向下箭头图标,而不是按 Enter 或单击“添加”按钮,以及 select“ Add As Link ”。

Please refer to this link .请参考此链接

如何访问列表<object>来自 appsettings.Json?<div id="text_translate"><p> 在我的 appsettings.json 我有一个这样的字段</p><pre> "MyFields": [ { "name":"one", "type":"type1" "parameters":[{"key":"url","value":"myurl.com"}, {"key":"data","value":"mydata"}] }, { "name":"two", "type":"type2"... .. and so on } ]</pre><p> 我制作了一个具有以下属性的 class Myfield:</p><pre> public class MyField { [JsonProperty] public string? name { get; set; } [JsonProperty] public string? type { get; set; } [JsonProperty] public IDictionary<string,string>? parameters { get; set; } }</pre><p> 我正在尝试使用配置在另一个 class 中访问它,如下所示:</p><pre> //config is the configuration var myFields = config.GetSection("MyFields").Get<List<MyField>>();</pre><p> 问题是 myFields 结果是空的。 但是当我通过消除“参数”字段来做同样的事情时,它就像一个魅力。</p><p> 我知道这与匹配不当有关,但能得到一些帮助会很棒。</p></div></object> - How can I access a List<object> from appsettings.Json?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从类库项目访问appsettings.json文件设置 - Access appsettings.json file setting from the class library project 如何在另一个项目或类库中使用 appsettings.json - How to use appsettings.json in another project or class library 从 DBContext Class 库中的主项目读取 appsettings.json - Read appsettings.json from Main project in DBContext Class library 从同一解决方案中的另一个项目访问 appsettings.json 很热吗? - Hot to access appsettings.json from another project in same solution? 从.NET 4.5.2项目访问appsettings.json - Access appsettings.json from .NET 4.5.2 project 如何访问列表<object>来自 appsettings.Json?<div id="text_translate"><p> 在我的 appsettings.json 我有一个这样的字段</p><pre> "MyFields": [ { "name":"one", "type":"type1" "parameters":[{"key":"url","value":"myurl.com"}, {"key":"data","value":"mydata"}] }, { "name":"two", "type":"type2"... .. and so on } ]</pre><p> 我制作了一个具有以下属性的 class Myfield:</p><pre> public class MyField { [JsonProperty] public string? name { get; set; } [JsonProperty] public string? type { get; set; } [JsonProperty] public IDictionary<string,string>? parameters { get; set; } }</pre><p> 我正在尝试使用配置在另一个 class 中访问它,如下所示:</p><pre> //config is the configuration var myFields = config.GetSection("MyFields").Get<List<MyField>>();</pre><p> 问题是 myFields 结果是空的。 但是当我通过消除“参数”字段来做同样的事情时,它就像一个魅力。</p><p> 我知道这与匹配不当有关,但能得到一些帮助会很棒。</p></div></object> - How can I access a List<object> from appsettings.Json? 在 EF 上下文中使用 appsettings.json 配置 - Using appsettings.json configuration in EF Context 未找到配置文件“appsettings.json”且不是可选的 - The configuration file 'appsettings.json' was not found and is not optional 在 Automapper 配置中使用 appsettings.json 值 - Using appsettings.json values in Automapper configuration appsettings.json 界面内的配置界面 - Configuration Interface inside an interface in appsettings.json
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM