appsettings.json
{ "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Verbose", "System": "Information", "Microsoft": "Information" }, "CustomSettings": { "UseDataCaching": true } } }
选项类
public class CustomSettings { public bool UseDataCaching { get; set; } }
startup.cs
public void ConfigureServices(IServiceCollection services) { ... services.AddOptions(); services.Configure(Configuration); ... } public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv) { // Set up configuration sources. var builder = new ConfigurationBuilder() .SetBasePath(appEnv.ApplicationBasePath) .AddJsonFile("appsettings.json") .AddEnvironmentVariables() .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true); if (env.IsDevelopment()) { // For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709 builder.AddUserSecrets(); // This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately. builder.AddApplicationInsightsSettings(developerMode: true); } builder.AddEnvironmentVariables(); Configuration = builder.Build(); }
调节器
protected IMemoryCache MemoryCache { get; } protected IOptionsCustomSettings { get; set; } public HomeController(IMemoryCache cache, IOptions customSettings) { MemoryCache = cache; CustomSettings = customSettings; }
customSettings.Value.UseDataCaching始终为false,即使在appsettings.json中我将其设置为true.
不确定我是否错过了非常明显的东西
编辑:添加启动构造函数
用法:
public IActionResult About() { var isUsingDataCache = CustomSettings.Value.UseDataCaching; ViewData["Message"] = "Your application description page."; return View(); }
编辑:改变启动采取2参数
示例项目不起作用 http://www.megafileupload.com/a2hn/WebApplication1.zip
不确定如何创建Configuration
对象,但要确保appsettings.json
在其管道中添加文件,也BasePath
为您的应用程序添加.例如:
public static IConfigurationRoot Configuration; // You can use both IHostingEnvironment and IApplicationEnvironment at the same time. // Instances of them are being injected at runtime by dependency injection. public Startup(IHostingEnvironment hostingEnv, IApplicationEnvironment appEnv) { // Set up configuration sources. var builder = new ConfigurationBuilder() .SetBasePath(appEnv.ApplicationBasePath) .AddJsonFile("appsettings.json") .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true); if (env.IsDevelopment()) { // For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709 builder.AddUserSecrets(); // This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately. builder.AddApplicationInsightsSettings(developerMode: true); } builder.AddEnvironmentVariables(); Configuration = builder.Build(); }
编辑:
尝试更改ConfigureServices
方法:
services.Configure(Configuration); // from this services.Configure (Configuration.GetSection("CustomSettings")); // to this
编辑2:
你可以尝试在控制器的构造函数中创建你的对象,而不是IOptions
像这样的泛型类型:
private CustomSettings _customSettings; public YourControllerName(CustomSettings customSettings) { _customSettings = customSettings; }
在Startup.cs
ConfigureServices
方法中,设置:
services.AddSingleton();
然后称之为:
public IActionResult About() { var isUsingDataCache = _customSettings.UseDataCaching; ViewData["Message"] = "Your application description page."; return View(); }
编辑3:
您CustomSettings
的json Logging
参数放在参数内.尝试将您的json更改为:
{ "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Verbose", "System": "Information", "Microsoft": "Information" } }, "CustomSettings": { "UseDataCaching": true } }
编辑4:
我实际上试过这个.创建了新的新项目,用Edit 3中的appsettings.json
数据替换了默认文件.在我放置的第一行方法里面:Startup.cs
ConfigureServices
public void ConfigureServices(IServiceCollection services) { services.Configure(Configuration.GetSection("CustomSettings")); ... }
最后,我在里面测试了它HomeController
: