Skip to content

This is to demonstrate the CI/CD pipeline in ASP.Net core without manually changing the appsettings,json file for various environments.

Notifications You must be signed in to change notification settings

manthangandhi/Asp.NetPublishDynamicAppSettings

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Asp.NetPublishDynamicAppSettings

This workaround is to demonstrate, changing appsettings.json dynamically for various environments during deployment.

While maintaining and deploying for various environments, there is a lot hassles managing those appsettings.json. So to avoid the those mind bursting hassles, we can leverage this workaround.

Let's begin,

Place all your required appsettings.json file into your project directory, such as appsettings.json , appsettings.developmet.json , appsettings.staging.json.

Modify your default Startup constructor as this,

public Startup(IConfiguration configuration, IHostingEnvironment env)
{
	var builder = new ConfigurationBuilder()
       	.SetBasePath(Directory.GetCurrentDirectory())
	       .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
       	.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
	       builder.AddEnvironmentVariables();
       	Configuration = builder.Build();
}

In the above code snippet, the below line is an important factor that helps us to achieve our goal.

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

This helps to load the appsettings.json name dynamically. So by this it will load the appsettings.json file based on the environment value that we set in our project solution.

To publish the project for different environments, use below command:

dotnet publish /p:Configuration=Release /p:EnvironmentName=Staging 

This will generate the publish files with a staging environment.

Hope it will save your valuable time. :)

About

This is to demonstrate the CI/CD pipeline in ASP.Net core without manually changing the appsettings,json file for various environments.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages