The file content is as follows
{ "Data": { "DefaultConnection": { "ConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=Books;Trusted_Connection=True;" } }, "Appsettings": { "Name": "XiaoLi", "Date": "2020-04-18", "age": "20" }, "url":"/weixin_42686768" }
Setting up configuration files
Right-click the project file - Properties - Change the value of the "Copy to Output Directory" item to "Always Copy"
Add a reference
using ; using ; using ; public static string GetConnectionString(string value) { var configurationBuilder = new ConfigurationBuilder() .SetBasePath(()) .AddJsonFile(""); //var configurationBuilder = new ConfigurationBuilder() // .Add(new JsonConfigurationSource { Path = "", ReloadOnChange = true }); IConfiguration config = (); string connectionString = config[value]; return connectionString; } // 1. Get the string corresponding to ConnectionStringva = "Data:DefaultConnection:ConnectionString"; // 2. Get the string corresponding to Nameva = "Appsettings:Name"; // 3. Get the string corresponding to urlva = "url"; GetConnectionString(va)
This is the article about reading JSON configuration files in vs C# project. For more related contents of reading JSON configuration files in C#, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!