Luke
Luke

Reputation: 855

Using active solution configuration to determine appsettings file with .NET 8

I've setup my active solution configuration to determine the appsettings.json file to use for the given build. I'm wondering if this is a recommended way the two in combination with each other, or if there an alternative best practice for managing configurations?

I deploy via publishing profiles within Visual Studio as I do not have a CICD pipeline.

There is an underlying assumption that Local is Debug and Staging and Prod are Release builds.

The appsettings files are registered as follows:

var configuration = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json")
        .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", true)
    .Build();

Launch settings:

"https": {
    "commandName": "Project",
    "launchBrowser": true,
    "launchUrl": "swagger",
    "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "$(ConfigurationName)"
    },
    "dotnetRunMessages": true,
    "applicationUrl": "https://localhost:7184;http://localhost:5081"
},

Project file to prevent configs from being published to unnecessary environments:

<ItemGroup>
    <Content Update="appsettings.Local.json">
        <CopyToOutputDirectory>Never</CopyToOutputDirectory>
    </Content>
    <Content Update="appsettings.Staging.json">
        <CopyToOutputDirectory>Never</CopyToOutputDirectory>
    </Content>
    <Content Update="appsettings.Prod.json">
        <CopyToOutputDirectory>Never</CopyToOutputDirectory>
    </Content>
    <Content Update="appsettings.$(ConfigurationName).json">
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
</ItemGroup>

My configuration files are:

enter image description here

And finally my configuration manager profiles are:

enter image description here

Upvotes: 0

Views: 77

Answers (0)

Related Questions