0

I am new to asp.net core and I am wondering how to put connectionStrings in separated file(json file) and ignore it so every developer can modify it to add his local connectionStrings?

In Asp.net I used to put connectionStrings in separated file (ex connectionStrings.config) and ignore it on git. Then inside web.config adding the following configuration:

  <connectionStrings configSource="connectionStrings.config" />

So could you please guide me to how I can achieve that using asp.net core?

3 Answers 3

2

use a ConfigureAppConfiguration in your Program class

WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.ConfigureAppConfiguration(builder =>
{
    builder.AddJsonFile("appsetting.json");
    builder.AddXmlFile("connectionStrings.config"));
});
Sign up to request clarification or add additional context in comments.

1 Comment

Yea it's working and I think I can add like followings .ConfigureAppConfiguration(builder => { builder.AddJsonFile("appsettings.json"); builder.AddJsonFile("connectionStrings.json")); });
0

With .NET Core or ASP.NET Core we don't use web.configs or app.configs for that purpose anymore. There is a new configuration-system built by Microsoft. By default, the webhost builds a configuration that includes these in that order

  1. appsettings.json, optional
  2. appsettings.{environment}.json, optional
  3. environment-variables

In your case you will want to build one manually.

For that you need to have a reference to (included by default for visual studio templates via meta-package)

  1. Microsoft.Extensions.Configuration
  2. Microsoft.Extensions.Configuration.Json
  3. Microsoft.Extensions.Configuration.EnvironmentVariables

Now instead of using the default configuration, build your own within your startup class.

public class Startup 
{
  // environment is being injected by the webhost
  public Startup(IHostingEnvironment environment)
  {
     var configuration = new ConfigurationBuilder()
                             .AddJsonFile(Path.Combine(AppContext.BaseDirectory, "appsettings.json"), optional: false) // u can change that if you want to be optional
                             .AddJsonFile(Path.Combine(AppContext.BaseDirectory, $"appsettings.{environment.environmentName}.json), optional: false)"
                             .AddJsonFile(Path.Combine(AppContext.BaseDirectory, "connectionsettings.json"), optional: false)
                             .Build();
  }
}

Then you can go ahead and access them kinda like before

configuration.GetConnectionString("MyConnectionString");

and your json file must look like that

"ConnectionStrings": {
   "MyConnectionString": "some-connectionstring-value"
}

Comments

0

You could also use .NET user secrets, see: https://learn.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-3.1&tabs=windows

These have huge benefits:

  • the secret file is outside git folder, so it will never be committed by mistake.
  • you can edit the secret file from Visual Studio and it looks similar to appsettings.json

Basically you have the secrets configuration only active for development (that is the default), and for production you would have another configuration source (Key vault or environment variable, etc)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.