这是我的appsettings.json。它位于没有startup.cs的类库中。我想知道是否有可能从那里获取变量以在本地使用?
{
"AccountsAddress": "http://localhost:55260/api/Accounts/",
"ApplicationUsersAddress": "http://localhost:55260/api/ApplicationUsers/"
}
public static class Manager
{
public static IConfiguration AppSetting { get; }
static Manager()
{
AppSetting = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build();
}
}
这是我图书馆的结构
答案 0 :(得分:3)
{
"AccountsAddress": "http://localhost:55260/api/Accounts/",
"ApplicationUsersAddress": "http://localhost:55260/api/ApplicationUsers/",
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}
创建一个myConfig.cs文件并粘贴以下代码。
using Microsoft.Extensions.Configuration;
static class ConfigurationManager
{
public static IConfiguration AppSetting { get; }
static ConfigurationManager()
{
AppSetting = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("YouAppSettingFile.json")
.Build();
}
}
现在您可以像这样使用它:
string value = ConfigurationManager.AppSetting["value_Key"];