Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

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.?

like image 266
jamheadart Avatar asked Nov 23 '25 17:11

jamheadart


1 Answers

You can clear them out:

var builder = WebApplication.CreateBuilder(args);
builder.Host.ConfigureAppConfiguration((hostingContext, config) =>
{
    config.Sources.Clear();
});
like image 52
Roman Ryzhiy Avatar answered Nov 26 '25 06:11

Roman Ryzhiy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!