Asp.Net Core 1.0.0:写入appsetting.json

时间:2016-07-26 09:32:55

标签: c# asp.net-core

我想在我的应用首次运行期间提供连接字符串,并通过常规表单将其保存到appsettings.json。我能够检测文件中是否有ConnectionString部分以及是否正确。如何保存提供的值?

1 个答案:

答案 0 :(得分:0)

我认为你正在寻找这样的东西:

var builder = new ConfigurationBuilder();
builder.SetBasePath(Directory.GetCurrentDirectory());
builder.AddJsonFile("appsettings.json");
var config = builder.Build();
config["ConnectionStrings:DefaultConnection"] = "MyConnectionStringHere";

//...    

var setting = config["ConnectionStrings:DefaultConnection"]; // also returns "MyConnectionStringHere"

有关配置文件和使用内置功能的更多信息,请查看Asp.Net Core Configuration