【发布时间】:2017-04-13 23:11:12
【问题描述】:
我正在尝试在我的 appsettings.json 文件中写入我的连接字符串并将其带入我的启动文件,但我不断得到一个值不能为空。 参数名称:连接字符串。我一直在使用各种示例,但似乎无法通过 ASP.NET 1.0 Core 启动类看到这个新设置。
Appsetting.json 文件:
{
"Data": {
"DefaultConnection": {
"ConnectionString": "Data Source=server;Initial Catalog=dbase;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
}
方法尝试 启动.cs
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public void ConfigureServices(IServiceCollection services)
{
var connStr = Configuration.GetConnectionString("DefaultConnection");
System.Console.WriteLine(connStr);
services.AddDbContext<DbContext>(options => options.UseSqlServer(connStr)); //error right here -- Null value
}
【问题讨论】:
标签: json asp.net-core asp.net-core-1.0 appsettings