我们如何将配置数据存储在 new asp.net vnext 中?
How can we store configuration data in new asp.net vnext?
我们如何在新的 asp.net vnext 中存储配置数据? web.config 仍然存在(system.web 已删除,所以没有任何 web.config,但我喜欢它)或使用新的 json 文件作为配置数据。
不确定您要存储的数据类型,但这对我有用。我创建了这个文件 myconfig.json 并以 JSON 格式存储我需要的数据。
这将注册您的配置文件,我在 startup.cs 页面中添加了这段代码。
Configuration = new Configuration()
.AddJsonFile("config.json")
.AddJsonFile("myconfig.json")
.AddEnvironmentVariables();
要从您的 JSON 文件中获取您需要的数据,您需要在您的代码中随时执行此操作。
IConfiguration Configuration = new Configuration().AddJsonFile("myconfig.json");
var jsonNodes = Configuration.Get("SomeJsonNode");
string someJsonString = Configuration.Get("someJsonString");
仅供参考:目前我测试了代码 json 数组不受支持。不确定他们现在是否解决了这个问题。
最佳配置是强类型选项的起点。所以你想在启动时做的是注册一个选项对象,用你从配置中读取的数据设置它,并在你的代码的其余部分从 DI 系统中获取它。
示例:
创建一个选项对象
public class ApplicationOptions
{
public bool MyOption { get; set; }
}
读取配置并设置选项对象然后在 DI 中注册它
public void Configure(IApplicationBuilder app)
{
// rest of setup
var applicationOptions = new ApplicationOptions();
string myOption;
if (configuration.TryGet("MyOption", out myOption) &&
myOption.Equals("True", StringComparison.OrdinalIgnoreCase))
{
applicationOptions.MyOption = true;
}
// For the scenario above also the code below will work.
// It is currently limited to desktop (ASPNET50, NET45) only.
// services.Configure<ApplicationOptions>(configuration);
services.AddInstance<ApplicationOptions>(applicationOptions);
// more setup
}
并在您的代码中解决它
public class MyController : Controller
{
public IActionResult Index(ApplicationOptions options)
{
// do something with the options
}
}
编辑:对于任何实用的可重用选项对象,设置它的代码可能会在启动之外发生 class。
在重大更改 (beta 6) 中,Microsoft.Framework.ConfigurationModel 程序集名称更改为 Microsoft.Framework.Configuration
这是修改后的启动class
public IConfiguration Configuration { get; set; }
public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
var configurationBuilder = new ConfigurationBuilder(appEnv.ApplicationBasePath).
.AddJsonFile("config.json")
.AddEnvironmentVariables();
Configuration = configurationBuilder.Build();
}
我们如何在新的 asp.net vnext 中存储配置数据? web.config 仍然存在(system.web 已删除,所以没有任何 web.config,但我喜欢它)或使用新的 json 文件作为配置数据。
不确定您要存储的数据类型,但这对我有用。我创建了这个文件 myconfig.json 并以 JSON 格式存储我需要的数据。
这将注册您的配置文件,我在 startup.cs 页面中添加了这段代码。
Configuration = new Configuration()
.AddJsonFile("config.json")
.AddJsonFile("myconfig.json")
.AddEnvironmentVariables();
要从您的 JSON 文件中获取您需要的数据,您需要在您的代码中随时执行此操作。
IConfiguration Configuration = new Configuration().AddJsonFile("myconfig.json");
var jsonNodes = Configuration.Get("SomeJsonNode");
string someJsonString = Configuration.Get("someJsonString");
仅供参考:目前我测试了代码 json 数组不受支持。不确定他们现在是否解决了这个问题。
最佳配置是强类型选项的起点。所以你想在启动时做的是注册一个选项对象,用你从配置中读取的数据设置它,并在你的代码的其余部分从 DI 系统中获取它。
示例:
创建一个选项对象
public class ApplicationOptions
{
public bool MyOption { get; set; }
}
读取配置并设置选项对象然后在 DI 中注册它
public void Configure(IApplicationBuilder app)
{
// rest of setup
var applicationOptions = new ApplicationOptions();
string myOption;
if (configuration.TryGet("MyOption", out myOption) &&
myOption.Equals("True", StringComparison.OrdinalIgnoreCase))
{
applicationOptions.MyOption = true;
}
// For the scenario above also the code below will work.
// It is currently limited to desktop (ASPNET50, NET45) only.
// services.Configure<ApplicationOptions>(configuration);
services.AddInstance<ApplicationOptions>(applicationOptions);
// more setup
}
并在您的代码中解决它
public class MyController : Controller
{
public IActionResult Index(ApplicationOptions options)
{
// do something with the options
}
}
编辑:对于任何实用的可重用选项对象,设置它的代码可能会在启动之外发生 class。
在重大更改 (beta 6) 中,Microsoft.Framework.ConfigurationModel 程序集名称更改为 Microsoft.Framework.Configuration 这是修改后的启动class
public IConfiguration Configuration { get; set; }
public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
var configurationBuilder = new ConfigurationBuilder(appEnv.ApplicationBasePath).
.AddJsonFile("config.json")
.AddEnvironmentVariables();
Configuration = configurationBuilder.Build();
}