如何解决dotnet core 3.1 Web API-名称“配置”在当前上下文中不存在
我正在使用dotnet core 3.1创建Web API,并且在获取配置设置时遇到了麻烦。这是错误:
The name 'Configuration' does not exist in the current context
startup.cs引用配置:
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
我看了一些例子,它们似乎都利用了
var connectionString = Configuration["ConnectionString"];
我在appsettings.json中看到了值,就像事实一样,还有一个用于配置的appsettings.Development.json文件。只是在访问它们时遇到麻烦。
我确信这很简单,但是由于某种原因,我花了很长时间才能找到答案。如果WeatherForecast模板中包含对配置值的引用,那就太好了。
解决方法
const result = await page.$x('//input[.//*contains(.,"France")]');
await page.click(result[0]);
确保您有var connectionString = Configuration.GetConnectionString("Default")
个部分,例如:
ConnectionStrings
编辑:
"ConnectionStrings": {
"Default": "Server=DESKTOP-L4QDV84;Database=Tournaments_MSA;Trusted_Connection=True;","HangfireConnection": "Server=DESKTOP-L4QDV84;Database=HangfireDB;Trusted_Connection=True;"
}
实例已经存在于服务集合中,因此您可以通过在控制器构造函数中添加参数来轻松地访问它。
IConfiguration
然后您可以在控制器的方法中使用它
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。