【发布时间】:2020-07-30 14:06:54
【问题描述】:
我正在将我的代码从 .NET CORE 2.2 迁移到 .NET CORE 3.1。从 appsettings.json 读取连接字符串时遇到以下错误
“'Configuration' 不包含 'GetConnectionString' 的定义”
我在 startup.cs 中使用下面的代码
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
}
我的 appsettings.json 如下
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"ConnectionStrings": {
"DefaultConnection": "Data Source=abc.net;Initial Catalog=xyz;User ID=paper;Password=pencil"
},
"AllowedHosts": "*",
"serverSigningPassword": "key",
"accessTokenDurationInMinutes": 2
}
有什么方法可以读取appsettings.json中的这个连接字符串和其他变量
【问题讨论】:
标签: asp.net-core-webapi asp.net-core-3.1 .net-core-3.1