1
votes

I am using .NET Core 3.0 ASP.net and my appsettings.json looks like this

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "Test":false 
}

Is there any way that I could configure a certain publish profile (pubxml) to change the value of Test to either true or false ?

1

1 Answers

0
votes

Yes, it's very easy. Currently, you are most likely using the Debug solution configuration for local development. Let's assume you also have Release as solution configuration and want to change certain settings upon publishing to Azure / with your publish profile.

Assuming you are using .NET Core 3.1 (also works with 2.x but the syntax is different), you can this code:

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
            webBuilder.UseEnvironment(Environment);
        });

public static string Environment
{
    get
    {
        string environmentName;
#if DEBUG
        environmentName = "development";
#elif RELEASE
        environmentName = "production";
#endif

        return environmentName;
    }
}

Also create a appsettings.product.json where you override specific values.

When you use your publish profile, simply select Release as solution configuration in the publish dialog. That way, you application will load appsettings.json for default values which will be overwritten by values in appsettings.production.json.

You can read more details in the docs.