我在appsettings/Config .json文件中设置了我的AppSettings数据,如下所示:
{
"AppSettings": {
"token": "1234"
}
}
我已经在网上搜索了如何从.json文件中读取AppSettings值,但我找不到任何有用的东西。
我试过了:
var configuration = new Configuration();
var appSettings = configuration.Get("AppSettings"); // null
var token = configuration.Get("token"); // null
我知道使用ASP.NET 4.0可以做到以下几点:
System.Configuration.ConfigurationManager.AppSettings["token"];
但是我如何在ASP.NET核心中做到这一点呢?
发布于 2015-09-23 23:27:56
首先: Microsoft.Framework.ConfigurationModel的程序集名称和命名空间已更改为Microsoft.Framework.Configuration。所以你应该使用:例如:
"Microsoft.Framework.Configuration.Json": "1.0.0-beta7"
作为project.json
中的依赖项。如果您没有安装7,请使用beta5或6。然后你就可以在Startup.cs
中做这样的事情了。
public IConfiguration Configuration { get; set; }
public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
var configurationBuilder = new ConfigurationBuilder(appEnv.ApplicationBasePath)
.AddJsonFile("config.json")
.AddEnvironmentVariables();
Configuration = configurationBuilder.Build();
}
如果您想要从config.json中检索变量,可以使用以下命令立即获取该变量:
public void Configure(IApplicationBuilder app)
{
// Add .Value to get the token string
var token = Configuration.GetSection("AppSettings:token");
app.Run(async (context) =>
{
await context.Response.WriteAsync("This is a token with key (" + token.Key + ") " + token.Value);
});
}
或者您可以创建一个名为AppSettings的类,如下所示:
public class AppSettings
{
public string token { get; set; }
}
并像这样配置服务:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.Configure<MvcOptions>(options =>
{
//mvc options
});
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
}
然后通过这样的控制器访问它:
public class HomeController : Controller
{
private string _token;
public HomeController(IOptions<AppSettings> settings)
{
_token = settings.Options.token;
}
}
发布于 2017-11-19 19:45:18
对于.NET Core2.0,情况发生了一些变化。启动构造函数接受配置对象作为参数,因此不需要使用ConfigurationBuilder
。下面是我的:
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<StorageOptions>(Configuration.GetSection("AzureStorageConfig"));
}
我的POCO是顶部提到的StorageOptions
对象:
namespace FictionalWebApp.Models
{
public class StorageOptions
{
public String StorageConnectionString { get; set; }
public String AccountName { get; set; }
public String AccountKey { get; set; }
public String DefaultEndpointsProtocol { get; set; }
public String EndpointSuffix { get; set; }
public StorageOptions() { }
}
}
这个配置实际上是我的名为AzureStorageConfig
的appsettings.json
文件的一部分
{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;",
"StorageConnectionString": "DefaultEndpointsProtocol=https;AccountName=fictionalwebapp;AccountKey=Cng4Afwlk242-23=-_d2ksa69*2xM0jLUUxoAw==;EndpointSuffix=core.windows.net"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
},
"AzureStorageConfig": {
"AccountName": "fictionalwebapp",
"AccountKey": "Cng4Afwlk242-23=-_d2ksa69*2xM0jLUUxoAw==",
"DefaultEndpointsProtocol": "https",
"EndpointSuffix": "core.windows.net",
"StorageConnectionString": "DefaultEndpointsProtocol=https;AccountName=fictionalwebapp;AccountKey=Cng4Afwlk242-23=-_d2ksa69*2xM0jLUUxoAw==;EndpointSuffix=core.windows.net"
}
}
我唯一要补充的是,由于构造函数已经更改,我还没有测试是否需要执行一些额外的操作才能加载appsettings.<environmentname>.json
而不是appsettings.json
。
发布于 2019-04-23 05:12:51
使用.NET Core2.2,并且以最简单的方式...
public IActionResult Index([FromServices] IConfiguration config)
{
var myValue = config.GetValue<string>("MyKey");
}
appsettings.json
是通过构造函数或动作注入自动加载和使用的,IConfiguration
上也有一个GetSection
方法。如果您只需要appsettings.json
,那么就不需要修改Startup.cs
或Program.cs
。
https://stackoverflow.com/questions/31453495
复制相似问题