简体   繁体   English

ASP.NET Core appsettings.json需要在磁盘上更新

[英]ASP.NET Core appsettings.json need to update on disk

I am currently working on project using asp.net core v2.0, and in my appsettings.json I have: 我目前正在使用asp.net core v2.0进行项目开发,在我的appsettings.json中,我具有:

"ContextSettings": {
    "ConnectionString": "Data Source={base-path}WahupaWeb.sqlite;Version=3;BinaryGUID=True;datetimeformat=CurrentCulture",
    "Provider": "System.Data.SQLite.EF6",
    "DropCreateDatabaseAlways": "false"
  }

I have a scenario where I need to update "data source" of connection string programmatically. 我有一种情况,我需要以编程方式更新连接字符串的“数据源”。

I am reading and trying to update JSON file using this code. 我正在阅读并尝试使用此代码更新JSON文件。

public class FileController : Controller
{
   private IConfigurationRoot Configuration { get; set; }    
   private void ConfigureConnectionString(string dbFileName)
   {
     var builder = new Microsoft.Extensions.Configuration.ConfigurationBuilder()
                      .SetBasePath(Environment.CurrentDirectory)
                      .AddJsonFile("appsettings.json", false, reloadOnChange: true);
     Configuration = builder.Build();
     Configuration["ContextSettings:ConnectionString"] = sqliteConnectionString;
   }
}

But it is not updating the file name "appsettings.json". 但它不会更新文件名“ appsettings.json”。

How to update appsettings.json file programmatically. 如何以编程方式更新appsettings.json文件。

Configuration is technically read-only. 配置在技术上是只读的。 In other words, you can't just change something on the Configuration object and have it write those changes to disk. 换句话说,您不能只更改Configuration对象上的内容并将其写入磁盘。 It's not set up that way. 它不是那样设置的。

If you need to programmatically change something like appsettings.json , you'd need to manually read the file, make your modifications and write those back to disk. 如果您需要以编程方式更改诸如appsettings.json之类的内容 ,则需要手动读取文件,进行修改,然后将其写回磁盘。 The same as you would for any file. 与处理任何文件相同。 Since it's JSON, you'd likely want to use an intermediary like JSON.NET to actually deserialize the JSON in the file, alter the object in memory, and then serialize it again, before writing to the file. 由于它是JSON,因此您可能希望使用JSON.NET之类的中介程序在文件中实际反序列化JSON,更改内存中的对象,然后再次对其进行序列化,然后再写入文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET Core appsettings.json 更新代码 - ASP.NET Core appsettings.json update in code 使用ASP.NET Core反序列化appsettings.json - Deserialize appsettings.json with ASP.NET Core ASP.NET核心EF代码首先appsettings.json - ASP.NET Core EF Code First appsettings.json ASP.NET Core appsettings.json 未加载正确的设置 - ASP.NET Core appsettings.json not loading the correct settings 用于 MSTest [ASP.NET Core 3.1] 的 appsettings.json - appsettings.json for MSTest [ASP.NET Core 3.1] AppSettings.json用于ASP.NET核心中的集成测试 - AppSettings.json for Integration Test in ASP.NET Core Serilog:RollingFile 在带有“appsettings.json”的 asp.net 核心中不起作用 - Serilog : RollingFile is not working in asp.net core with 'appsettings.json' 为 ASP.NET Core 配置引用 appsettings.json 中的另一个 json 文件 - Reference another json file in appsettings.json for ASP.NET Core configuration ASP.NET Core 3.1 将 appsettings.json 中的对象数组注入到注入的服务中 - ASP.NET Core 3.1 injecting array of objects from appsettings.json into an injected service 从ASP.NET CORE Web应用程序中的appsettings.json加载Hangfire配置 - Loading Hangfire configuration from appsettings.json in ASP.NET CORE web app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM