3

I have a main web application and a Web API application. (The first application calls the API of the second application.) When running locally, Visual Studio is configured to lauch both applications.

My appsettings.development.json file overrides the URL of the API website when running locally. This is working fine.

But now another developer is running these projects on his system. When he does, the URL of the API website is different than on my system.

How can this developer configure a different URL in appsettings.development.json without overwriting my settings when he checks it in?

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466

4 Answers4

3

I assume you use a Code versioning control tool. I will assume you use git. If it's not the case, you SHOULD use a Versioning Control tool, if it's not git, check how you can "ignore" specific files.

You should not track the appSettings.development.json and add it to your .gitignore (see this thread if appSettings.development.json is already tracked and you need to clean it.)

Then each developer will have his own appSettings.development.json and it will not be overwritten

HTH

Cyril ANDRE
  • 124
  • 9
1

You could use user secrets for this. It's basically a JSON config file that is outside the project folder so it won't get checked in to version control. I've used this for local DB connection strings/API URLs etc.

In Visual Studio you can right click the project and click Manage user secrets to open the user secrets file.

juunas
  • 54,244
  • 13
  • 113
  • 149
1

You could use different Launch Settings, so you can create separate appSetting file for each developer.

You can create appSettings.Developer1.json and appSettings.Developer2.json for both developer, then you will need to modify launchSetting.json as below to make it work for both developer

below is example launchSetting.json

{
  "iisSettings": {
    ...
  },
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "profiles": {
    "Developer 1": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Developer1"
      }
    },
    "Developer 2": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Developer2"
      }
    }
  }

You can choose which setting to use when running application

0

Just stumbled upon the same thing. I probably solved it in a nasty wat but it seems to work.

Add launchsettings, and name the environment to something starting with "Development"

"ASPNETCORE_ENVIRONMENT": "Development.ServerDB"

Then create a new aspsettings file named

appsettings.Development.ServerDB.json

Now in the Program.cs, before builder.Build() Change the environment back to Development.

if (builder.Environment.EnvironmentName.StartsWith("Development"))
{
    builder.Environment.EnvironmentName = "Development";
}

var app = builder.Build();

This will load the appsettings.Development.ServerDB.json file and it will still be in Development mode (for better error messages)

I did not extend it yet, but this way you could also add different appsettings per developer etc.

Advantage is that these files can (or should I say must) be checked in into version control, allowing you to trace back history as well

bart s
  • 5,068
  • 1
  • 34
  • 55