积极答复者
.NET Core 如何在其它类库读取 appsettings.json 或者其它配置文件?

问题
答案
-
你好,
下面的文章提供了3步 来访问appsettings.json 里面的内容。
#Json
{ "ServiceSettings": { "NewsMainUrl": "https://newsapi.org", "NewsApiKey": "abc" }, "BALSettings": { "Source": "xyz", "FilterTerms": "abc;def;" } }
1. 建一个model class 来映射appsettings.json里面的字段
namespace Tweet.Entities { public class BALSettings { public string Source { get; set; } public string FilterTerms { get; set; } } public class ServiceSettings { public string NewsMainUrl { get; set; } public string NewsApiKey { get; set; } } }
2. 通过DI容器注册 appsettings.json 到相关的 model classes 里面。
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.Configure<BALSettings>(Configuration.GetSection("BALSettings")); services.Configure<ServiceSettings>(Configuration.GetSection("ServiceSettings")); }
3. 在类库项目中访问 appsettings.json section.
注意: Microsoft.Extensions.Options 要通过Nuget 来安装。
PM> Install-Package Microsoft.Extensions.Options
using Microsoft.Extensions.Options; public class NewsService : INewsService { private readonly IOptions<ServiceSettings> _serviceSettings; public NewsService(IOptions<ServiceSettings> serviceSettings) { _serviceSettings = serviceSettings; } public string composeUrl() { return _serviceSettings.Value.NewsMainUrl + "&apiKey=" + _serviceSettings.Value.NewsApiKey; } }
Best regards,
Zhanglong
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- 已编辑 Zhanglong WuMicrosoft contingent staff, Moderator 2018年9月3日 5:22
- 已标记为答案 peify 2018年9月3日 14:42
全部回复
-
你好,
下面的文章提供了3步 来访问appsettings.json 里面的内容。
#Json
{ "ServiceSettings": { "NewsMainUrl": "https://newsapi.org", "NewsApiKey": "abc" }, "BALSettings": { "Source": "xyz", "FilterTerms": "abc;def;" } }
1. 建一个model class 来映射appsettings.json里面的字段
namespace Tweet.Entities { public class BALSettings { public string Source { get; set; } public string FilterTerms { get; set; } } public class ServiceSettings { public string NewsMainUrl { get; set; } public string NewsApiKey { get; set; } } }
2. 通过DI容器注册 appsettings.json 到相关的 model classes 里面。
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.Configure<BALSettings>(Configuration.GetSection("BALSettings")); services.Configure<ServiceSettings>(Configuration.GetSection("ServiceSettings")); }
3. 在类库项目中访问 appsettings.json section.
注意: Microsoft.Extensions.Options 要通过Nuget 来安装。
PM> Install-Package Microsoft.Extensions.Options
using Microsoft.Extensions.Options; public class NewsService : INewsService { private readonly IOptions<ServiceSettings> _serviceSettings; public NewsService(IOptions<ServiceSettings> serviceSettings) { _serviceSettings = serviceSettings; } public string composeUrl() { return _serviceSettings.Value.NewsMainUrl + "&apiKey=" + _serviceSettings.Value.NewsApiKey; } }
Best regards,
Zhanglong
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- 已编辑 Zhanglong WuMicrosoft contingent staff, Moderator 2018年9月3日 5:22
- 已标记为答案 peify 2018年9月3日 14:42