如何从ASP.NET Core中的.json文件读取AppSettings值 [英] How to read AppSettings values from a .json file in ASP.NET Core
问题描述
我已经在文件appsettings / Config .json中设置了我的AppSettings数据,如下所示:
I have set up my AppSettings data in file appsettings/Config .json like this:
{
"AppSettings": {
"token": "1234"
}
}
我已经在网上搜索了如何从.json文件中读取AppSettings值的信息,但没有得到任何有用的信息。
I have searched online on how to read AppSettings values from .json file, but I could not get anything useful.
我尝试过:
var configuration = new Configuration();
var appSettings = configuration.Get("AppSettings"); // null
var token = configuration.Get("token"); // null
我知道使用ASP.NET 4.0可以做到:
I know with ASP.NET 4.0 you can do this:
System.Configuration.ConfigurationManager.AppSettings["token"];
但是如何在ASP.NET Core中做到这一点?
But how do I do this in ASP.NET Core?
推荐答案
这有些曲折。我已修改此答案以使其与 ASP.NET Core 2.0 保持最新(截至2018年2月26日)。
This has had a few twists and turns. I've modified this answer to be up to date with ASP.NET Core 2.0 (as of 26/02/2018).
此大部分取自官方文档:
要使用ASP.NET应用程序中的设置,建议仅在应用程序的中实例化
类。然后,使用选项模式访问各个设置。假设我们有一个配置
启动 appsettings.json
文件,如下所示:
To work with settings in your ASP.NET application, it is recommended that you only instantiate a Configuration
in your application’s Startup
class. Then, use the Options pattern to access individual settings. Let's say we have an appsettings.json
file that looks like this:
{
"MyConfig": {
"ApplicationName": "MyApp",
"Version": "1.0.0"
}
}
我们有一个表示配置的POCO对象:
And we have a POCO object representing the configuration:
public class MyConfig
{
public string ApplicationName { get; set; }
public int Version { get; set; }
}
现在我们在 Startup.cs中构建配置
:
public class Startup
{
public IConfigurationRoot Configuration { get; set; }
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
Configuration = builder.Build();
}
}
请注意, appsettings.json
将在.NET Core 2.0中默认注册。如果需要,我们还可以注册 appsettings。{Environment} .json
每个环境的配置文件。
Note that appsettings.json
will be registered by default in .NET Core 2.0. We can also register an appsettings.{Environment}.json
config file per environment if needed.
如果需要要将配置注入到控制器中,我们需要在运行时注册它。我们通过 Startup.ConfigureServices
:
If we want to inject our configuration to our controllers, we'll need to register it with the runtime. We do so via Startup.ConfigureServices
:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
// Add functionality to inject IOptions<T>
services.AddOptions();
// Add our Config object so it can be injected
services.Configure<MyConfig>(Configuration.GetSection("MyConfig"));
}
我们像这样注入它:
public class HomeController : Controller
{
private readonly IOptions<MyConfig> config;
public HomeController(IOptions<MyConfig> config)
{
this.config = config;
}
// GET: /<controller>/
public IActionResult Index() => View(config.Value);
}
完整的启动
类:
public class Startup
{
public IConfigurationRoot Configuration { get; set; }
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
Configuration = builder.Build();
}
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
// Add functionality to inject IOptions<T>
services.AddOptions();
// Add our Config object so it can be injected
services.Configure<MyConfig>(Configuration.GetSection("MyConfig"));
}
}
这篇关于如何从ASP.NET Core中的.json文件读取AppSettings值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!