我正在使用C#和Core .NET构建一个类库.我正在尝试使用config.json文件中的配置.以下是该文件的内容:
config.json
{
"emailAddress":"someone@somewhere.com"
}
Run Code Online (Sandbox Code Playgroud)
为了尝试使用config.json我的配置,我Microsoft.Framework.ConfigurationModel.Json在我的project.json文件中引用.在我的代码中,我有以下内容:
MyClass.cs
using Microsoft.Framework.ConfigurationModel;
public class MyClass
{
public string GetEmailAddress()
{
// return ConfigurationManager.AppSettings["emailAddress"]; This is the approach I had been using since .NET 2.0
return ?; // What goes here?
}
}
Run Code Online (Sandbox Code Playgroud)
从.NET 2.0开始,我一直在使用ConfigurationManager.AppSettings["emailAddress"].但是,我现在正试图通过以下方式学习如何做到这一点IConfiguration.我的问题是,这是一个类库.因此,我不确定配置文件的加载方式,位置或时间.在传统的.NET中,我只需要为ASP.NET项目命名文件web.config,为其他项目命名app.config.现在,我不确定.我有一个ASP.NET MVC 6项目和一个XUnit项目.所以,我正在试图弄清楚如何使用config.json在这两种情况下使用.
谢谢!