Skip to content

How to add more configuration files to a web application? #57304

Answered by davidfowl
paulomorgado asked this question in Q&A
Discussion options

You must be logged in to vote

You can mutate the list of sources, here's an example of inserting it first. 

var builder = WebApplication.CreateBuilder(args);

builder.Configuration.InsertBefore(c =>
{
    c.AddJsonFile("MyInitialSource.json", optional: true);
});

var app = builder.Build();

app.MapGet("/", () => "Hello World!");

app.Run();

public static class ConfigurationExtensions 
{
    public static ConfigurationManager InsertBefore(this ConfigurationManager config, Action<ConfigurationManager> action)
    {
        // Get all existing sources and make a copy
        var existing = config.Sources.ToArray();
        // Clear theses sources
        config.Sources.Clear();
        // Mutate configuration
        a…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@paulomorgado
Comment options

@davidfowl
Comment options

Answer selected by paulomorgado
@paulomorgado
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants