1

I have to set a configurable value in CreateHostBuilder either from appsettings or any way; I just tried using appsettings

public static IHostBuilder CreateHostBuilder(string[] args) =>
                 Host.CreateDefaultBuilder(args)
                    .ConfigureWebHostDefaults(webBuilder =>
                    {
                        webBuilder.UseStartup<Startup>();
                        webBuilder.ConfigureAppConfiguration((hostingContext, config) =>
                        {
                            var url = _configuration.GetSection("url");
                            config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
                             .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).AddOcelot(// Set url here)

                               .AddEnvironmentVariables();
                        });

                    });

But url value always returns as null. Does anyone has better solution to get appsetings in ConfigureAppConfiguration or any better idea to use any other idea to set configuration value?

1
  • I am wonderig where _configuration from? and what you need url for? Commented Oct 14, 2021 at 22:58

2 Answers 2

5

You can change like below:

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
    .ConfigureWebHostDefaults(webBuilder =>
    {
        webBuilder.UseStartup<Startup>();
        webBuilder.ConfigureAppConfiguration((hostingContext, config) =>
        {
            var settings = config.Build();
            var url = settings.GetSection("url").Value;

        });

    });
Sign up to request clarification or add additional context in comments.

Comments

1

try this

Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                })
             .ConfigureServices((hostContext, services) =>
             {
                 IConfiguration configuration = hostContext.Configuration;
                  var url = configuration.GetSection("url");
             });

1 Comment

hey thanks but I have to get that Iconfiguration in configureAppConfiguration()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.