Home > database >  How can I clear defaults from WebApplicationBuilder.CreateBuilder()?
How can I clear defaults from WebApplicationBuilder.CreateBuilder()?

Time:12-17

In a .NET 6 / 7 web project, the default builder is used on the very first line

var builder = WebApplicationBuilder.CreateBuilder();

And this builder then has configs such as "appsettings.json" and "appsettings.Development.json" included in it.

The documentation says:

... initializes a new instance of the WebApplication class with preconfigured defaults

But I don't want those defaults. I'd like to get rid of appsettings and appsettings.development.

I used to be able to do this in lower versions of .NET using the builder.Host with Clear or something but now it seems I can only add to those default configs? Or do I have to use the older class libraries to get full control of the configs etc.?

CodePudding user response:

You can clear them out:

var builder = WebApplication.CreateBuilder(args);
builder.Host.ConfigureAppConfiguration((hostingContext, config) =>
{
    config.Sources.Clear();
});

CodePudding user response:

As mentioned here ConfigurationManager returned by WebApplicationBuilder.Configuration property allows to setup the config, you can clear default configuration sources from it:

builder.Configuration.Sources.Clear();
  • Related