0

I want the configuration values from appsettings.json to remain unchanged when running on localhost. I have already added separate appsettings.{env}.json files to other environments. I want to use appsettings.Development.json file only for Dev environment not for localhost.

Do i need to create separate appsettings.Localhost.json file? Or Can I create new profile (Localhost) like below? Please suggest.

"Localhost": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7055;http://localhost:5065",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Localhost"
      }
    }

Below is my Program.cs

var builder = WebApplication.CreateBuilder(args);
{
    // Environment configuration
    var configuration = builder.Configuration;

    var env = builder.Environment;

    configuration
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);


    builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssemblyContaining<Program>());
    builder.Services.AddHttpClient();

    builder.Services.AddApplication();
    builder.Services.AddInfrastructure(builder.Configuration);

    builder.Services.AddHelpers();

    builder.Services.AddControllers()
       // Configures the JSON serialization options for controllers.
       .AddJsonOptions(options =>
        {
            options.JsonSerializerOptions.PropertyNamingPolicy = null;
        });

    builder.Services.Configure<ApiBehaviorOptions>(options =>
    {
        options.SuppressModelStateInvalidFilter = true;
    });

} 
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Rakesh Kumar
  • 2,701
  • 9
  • 38
  • 66
  • is [this answer](https://stackoverflow.com/a/60961168) what you want? – Tiny Wang May 22 '23 at 05:33
  • @TinyWang Not exactly!, I know how can we add appsettings for different environment. But my question is how can we keep appsettings values if I am running on my localhost, because I will use `appsettings.Development.json` file for live dev environment, not for localhost. So I don't want to this settings file for localhost environment. – Rakesh Kumar May 22 '23 at 06:17
  • do you think change the environment variable like the answer mentioned is a workaround for you to stop using `appsettings.Development.json` for localhost? – Tiny Wang May 22 '23 at 07:09
  • @TinyWang I have added new profile name with 'Localhost' and added check if environment variable is Localhost then it should not override `appsettings.json` values. Is that correct approach? – Rakesh Kumar May 23 '23 at 04:50
  • yeah, I'm afraid yes. I'm not very sure if I understood your requirement right. But I think if you had a list of appsettings.xxx.json, and you want one of them(not the develop one but the localhost one) only be used when you run the app in your local machine, you should change the environment variable(test to change in launch setting/vs envrionment/xxx) like what the answer said. – Tiny Wang May 23 '23 at 05:19

1 Answers1

1

I have a test in my side, I add a configuration in appsettings.json. When I changed the launch settings to Localhost, then it would read variable in appsettings.Localhost.json

enter image description here

enter image description here

Tiny Wang
  • 10,423
  • 1
  • 11
  • 29
  • Yes! it works for me. Thanks for posting this answer. I hope this is right approach to prevent `appsettings.json` values from being overridden when running on localhost. – Rakesh Kumar May 23 '23 at 09:11
  • Yes, you can have a try sir, and let us know if it worked or not. – Tiny Wang May 23 '23 at 09:19