I have 3 ASP.NET CORE projects in one solution and I want to share connection string information in one config file like appsettings.json across these projects.
How can I do this in Visual Studio 2015 ?
Of course, we can add and use multiple appsettings. json files in ASP.NET Core project. To configure and read data from your custom json files, you can refer to the following code snippet. Host.
Add Json File After adding the file, right click on appsettings. json and select properties. Then set “Copy to Ouptut Directory” option to Copy Always. Add few settings to json file, so that you can verify that those settings are loaded.
We simply need to inject a dependency Constructor with the Startup class for reading the values. With . NET Core, we can use IServiceCollection as single tone object or more specifically a configuration interface IConfiguration for reading JSON key value in our code.
You can use absolute path on each project like this(I assume appsettings.json
file is in the solution root directory(like global.json) ):
var settingPath = Path.GetFullPath(Path.Combine(@"../../appsettings.json")); // get absolute path
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile(settingPath);
see https://github.com/aspnet/Configuration/issues/440
Another option is to use user secrets and have the apps share the same app id.
As a bonus, you get the keep the connection strings outside of your app and decrease the chances that they will leak because they were pushed in source control.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With