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<CustomSettings>(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 IOptions<CustomSettings> CustomSettings { get; set; }
public HomeController(IMemoryCache cache, IOptions<CustomSettings> 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
答案 0 :(得分:5)
不确定如何创建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<CustomSettings>(Configuration); // from this
services.Configure<CustomSettings>(Configuration.GetSection("CustomSettings")); // to this
编辑2:
您是否可以尝试在控制器的构造函数内创建对象,而不是像IOptions
这样的泛型类型:
private CustomSettings _customSettings;
public YourControllerName(CustomSettings customSettings)
{
_customSettings = customSettings;
}
在Startup.cs
ConfigureServices
方法中,设置:
services.AddSingleton<CustomSettings>();
然后称之为:
public IActionResult About()
{
var isUsingDataCache = _customSettings.UseDataCaching;
ViewData["Message"] = "Your application description page.";
return View();
}
编辑3:
您的json的CustomSettings
参数位于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<CustomSettings>(Configuration.GetSection("CustomSettings"));
...
}
位置:
HomeController
最后,我在public void click(String locatorValue)
{
WebElement foundElement = driver.findElement(By.id(locatorValue));
foundElement.click();
}
内进行了测试: