2

I have plenty of connection strings that I need to use them in different scenarios like Live, Report and Test just by changing solution configuration in Visual Studio. I spent a lot of time on it, but I can't figure out a solution. Any idea about it?

Thanks in advance.

Ali Soltani
  • 9,589
  • 5
  • 30
  • 55
  • **See Also**: [Automatically set appsettings.json for dev and release environments in asp.net core?](https://stackoverflow.com/questions/46364293/automatically-set-appsettings-json-for-dev-and-release-environments-in-asp-net-c) – KyleMit Mar 31 '20 at 19:46

2 Answers2

3

You can have different appsettings for different environments like:

  • appsettings.Development.json
  • appsettings.Staging.json
  • appsettings.Production.json
  • etc. ...

and then add different profiles with different environment to launch your application in launchSettings.json

launchSettings.json

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:49364",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express Development": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express Staging": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Staging"
      }
    },
    "IIS Express Production": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Production"
      }
    }
  }
}
KyleMit
  • 30,350
  • 66
  • 462
  • 664
Kahbazi
  • 14,331
  • 3
  • 45
  • 76
2

In addition to @Kahbazi answer I would like to update my answer like this

In your case You can use Options pattern in ASP.NET Core

Example you have appsetting.json that have structure like this

"Development": {
    "Url": "some string"
},
"Stage": {
      "Url": "some string"
},
"Prod": {
      "Url": "some string"
}

I will need to define a model class like this to store information

public class Development
{
  public string Url{ get; set; }
}

So we have the the config structure. The last thing we need is register inside Startup.cs:

services.Configure<Development>(configuration.GetSection("Development"));

So you can use like this:

private readonly IOptions<Development> _webAppUrl;

public EmailSender(IOptions<Development> _webAppUrl)
{
  _webAppUrl = _webAppUrl;
}

_webAppUrl.Value.Url;

This is how Microsoft setup multiple appsetting.json files. You can take a look at because it's kinda long post

Here is how I config using json file base on environment:

public Startup(
    IConfiguration configuration,
    IHostingEnvironment hostingEnvironment)
{
    _configuration = configuration;
    _hostingEnvironment = hostingEnvironment;

    var builder = new ConfigurationBuilder();

    if (_hostingEnvironment.IsDevelopment())
    {
        builder.AddUserSecrets<Startup>();
    }
    else
    {
        builder
            .SetBasePath(hostingEnvironment.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{hostingEnvironment.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables();
    }
}
KyleMit
  • 30,350
  • 66
  • 462
  • 664
Tony Ngo
  • 19,166
  • 4
  • 38
  • 60