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