ASP.NET Core package for single-call environment-aware config loading with auto-scaffolded publish profiles and appsettings.
πΉπ· TΓΌrkΓ§e iΓ§in README.tr.md
dotnet add package EnvironmentConfiguratorvar builder = WebApplication.CreateBuilder(args);
builder.AddEnvironmentConfiguration(); // loads appsettings.{Environment}.json + env variables
var app = builder.Build();
app.Run();On the first build after install, missing config files are scaffolded automatically into your project.
- Loads
appsettings.jsonβappsettings.{Environment}.jsonβ environment variables in one line. - On the first
dotnet buildafter install, auto-copies missing configuration files (never overwrites existing ones). - Integrates into existing projects in seconds via
dotnet add package. - Targets .NET 8 and later (incl. net9 / net10).
- .NET 8.0 or later
- ASP.NET Core
dotnet add package EnvironmentConfiguratorAvailable on nuget.org.
using EnvironmentConfigurator;
var builder = WebApplication.CreateBuilder(args);
builder.AddEnvironmentConfiguration();
var app = builder.Build();
app.Run();AddEnvironmentConfiguration runs on the .NET 8 IHostApplicationBuilder; use it directly with WebApplicationBuilder.
If you don't have access to the builder, or work at the IConfigurationBuilder level:
builder.Configuration.AddEnvironmentJsonFiles(builder.Environment);builder.AddEnvironmentConfiguration(options =>
{
options.BaseSettingsOptional = false; // throw if appsettings.json is missing (default: false)
options.ReloadOnChange = true; // reload on file change (default: true)
options.IncludeEnvironmentVariables = true; // add the env variables source (default: true)
options.BasePath = "config"; // if JSON files live in another folder
});After installing the package and building for the first time, the following files not already present in your project are generated automatically:
appsettings.Beta.json
appsettings.Production.json
web.config
Properties/PublishProfiles/Beta-FolderProfile.pubxml
Properties/PublishProfiles/Development-FolderProfile.pubxml
Properties/PublishProfiles/Production-FolderProfile.pubxml
- Existing files are never overwritten β only missing ones are added, your edits are preserved.
- Generated files are logged in the build output:
EnvironmentConfigurator: scaffolded ... - The
.pubxmltemplates contain no project-specificProjectGuidβEnvironmentNameis set in each file.
Add to your .csproj:
<PropertyGroup>
<EnvironmentConfiguratorScaffold>false</EnvironmentConfiguratorScaffold>
</PropertyGroup>The ASPNETCORE_ENVIRONMENT variable decides which appsettings.{X}.json is loaded.
| Scenario | Where to set it |
|---|---|
| Debug / local | Properties/launchSettings.json β profile environmentVariables |
| Command line | ASPNETCORE_ENVIRONMENT=Beta dotnet run |
| IIS / Web Deploy | web.config β <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Production" /> |
Override logic: appsettings.json holds shared settings; appsettings.{Environment}.json overrides only the keys that change for that environment.
launchSettings.json example:
{
"profiles": {
"Beta": {
"commandName": "Project",
"launchUrl": "swagger",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Beta" }
}
}
}In Visual Studio, use the publish profiles (.pubxml). Each profile sets its environment via <EnvironmentName>; the correct configuration is selected at publish time.
<Project>
<PropertyGroup>
<EnvironmentName>Beta</EnvironmentName>
<PublishProvider>FileSystem</PublishProvider>
<PublishUrl>bin\Release\net8.0\publish\</PublishUrl>
<WebPublishMethod>FileSystem</WebPublishMethod>
<TargetFramework>net8.0</TargetFramework>
<SelfContained>false</SelfContained>
</PropertyGroup>
</Project>EnvironmentConfiguratorApi is a fully working live example. It consumes the package via ProjectReference and ships with per-environment appsettings files, publish profiles and launchSettings.json. The /test/environment-name endpoint returns the active environment name.
cd EnvironmentConfiguratorApi
ASPNETCORE_ENVIRONMENT=Beta dotnet run
# in another terminal:
curl http://localhost:5000/test/environment-name # β "Beta"EnvironmentConfigurator.sln
βββ EnvironmentConfigurator/ β NuGet package (code + scaffold target + templates)
βββ EnvironmentConfiguratorApi/ β sample API consuming the package
βββ EnvironmentConfigurator.Tests/ β xUnit tests
dotnet pack EnvironmentConfigurator -c Release -o ./artifacts
# β artifacts/EnvironmentConfigurator.1.0.3.nupkgMIT β see LICENSE.txt.