7

I am starting asp.net core 2.1 coming from .NET and wondering how do I make multiple AppSetting.json files?

Before we had the webconfig that you could have webconfig.debug, webconfig.prod and etc.

What is the core equivalent to that?

chobo2
  • 83,322
  • 195
  • 530
  • 832

1 Answers1

11

By default ASP.NET Core will attempt to load an additional appsettings.<EnvironmentName>.json file. Using the default environment names available, this allows you to create the following files:

  • appsettings.json - loaded regardless of the environment name
  • appsettings.Development.json - loaded only when the environment name is Development
  • appsettings.Staging.json - loaded only when the environment name is Staging
  • appsettings.Production.json - loaded only when the environment name is Production

The name of the environment is usually controlled via the ASPNETCORE_ENVIRONMENT environment variable or via launchSettings.json when developing (checkout the docs).

Take a look at the documentation for configuration for more info regarding this topic.

Henk Mollema
  • 44,194
  • 12
  • 93
  • 104
  • Ok, yea this is what I was looking for, I see in VS studio 2017 Preview, that "appsettings.Development.json" is made for me but I don't see a way to add .Staging or Production. Also Does Development.json map to the "Debug" Configuration? – chobo2 May 17 '18 at 17:48
  • Just add them as new files in VS, e.g. right click on the project -> Add -> New Item -> App Setting File -> call it `appsettings.Production.json` (or `Staging`). – Henk Mollema May 17 '18 at 17:49
  • 1
    Wow, that is not intuitive at all. If I am using these default names, is there still a need to create launchSetting.json? – chobo2 May 17 '18 at 17:51
  • No, it does not map to the _build_ configuration of your project (e.g. `Debug` or `Release`). By default there is no such thing, you could create it yourself though. – Henk Mollema May 17 '18 at 17:51
  • Visual Studio probably created a `launchSettings.json` file as you created the project. – Henk Mollema May 17 '18 at 17:52
  • Ah, your right, thought it would be root level file, but it was in Properties folder. I am bit confused(I don't like how this article mixes 1.0 and 2.0). Do I need to call AddJsonFile or just leave "CreateDefaultBuilder" that is in Program.cs. I am also unclear on how I can change the environment variable for testing. Do you just add a new profile and switch to that? : https://learn.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-2.1 – chobo2 May 17 '18 at 18:09
  • For ASP.NET Core 2.0 apps calling `CreateDefaultBuilder` will do it. For unit tests you can mock up your own instance of `IHostingEnvironment` and have the `EnvironmentName` property return the appropriate name. For integration tests you can call `UseSetting(WebHostDefaults.EnvironmentKey, "Staging")` (or any name) on the web host builder to specify the environment name. – Henk Mollema May 17 '18 at 18:17
  • Thanks for your great answer @HenkMollema – jcvegan May 17 '18 at 19:48
  • @jcvegan you're welcome, you might want to consider marking it as an answer :) – Henk Mollema May 18 '18 at 08:08
  • Do I need to include the launchSettings.json when publishing my site to staging instance? My staging and production is in the same machine, I'm wondering how to tell the site that it is a Staging environment. – Setrákus Ra Sep 07 '18 at 13:55
  • @SetrákusRa I'd go with specifying the `ASPNETCORE_ENVIRONMENT` environment variable for deployments to a server. – Henk Mollema Sep 07 '18 at 14:46
  • @HenkMollema - I found an answer to my question here https://stackoverflow.com/questions/31049152/publish-to-iis-setting-environment-variable . The environment variable can be set in the IIS. – Setrákus Ra Sep 10 '18 at 13:24