.NET Tips and Tricks

Blog archive

Using the Right Settings File in ASP.NET Core

In ASP.NET Core, the appsettings.json file gives you a place to store your application's configuration settings. You can have multiple versions of your appsettings file, one for each of your environments (test, production, etc.). The various files are identified by including the environment name in the file name, like this:

  • appsettings.development.json
  • appsettings.staging.json
  • appsettings.production.json

The tools I described in that earlier article for retrieving settings from appsettings will automatically pick the version of appsettings passed based on the value in the ASPNETCORE_ENVIRONMENT global variable. You can set that variable to any value you want, but three settings are supported out of the box: Testing, Staging and Production (the default is "Production").

The easiest way to check that setting is by retrieving the IHostingEnvironment object and checking the object's IsDevelopment, IsProduction and IsStaging properties. As an example, the default code in the Startup class's Configure method accepts the IHostingEnvironment object and uses those properties like this:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
   if (env.IsDevelopment())
   {
      app.UseDeveloperExceptionPage();
   }

You'll most likely control ASPNETCORE_ENVIRONMENT through the launchsettings.json file. If that file exists, ASP.NET Core scans the file's profiles entry looking for an entry with a commandName property that matches how the application was started. An entry like the following in launchsettings will, when the application is launched in IIS Express, set ASPNETCORE_ENVIRONMENT to "Staging" and, as a result, your tools will read the appsettings.staging.json file:

"profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Staging"
      }

I wouldn't try to edit the file yourself. Instead, open your project's Properties and pick the Debug tab. The form for that tab will let you manage the profiles in the launchsettings.json file without having to edit the json file yourself.

Posted by Peter Vogel on 03/22/2019 at 9:04 AM


.NET Insight

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.

Upcoming Events