1 year ago

#279713

test-img

Pika

ASP.NET Core , .NET 6> Overwirte appsettings based on HttpContext / Current URL

In my ASPNET Core App, how to overload / overwrite appsettings based on current url ?

I have actually one application "my app", in .NET 6. I host this application in IIS, 2 times :

http://local.domain1.com/myapp

http://local.domain2.com/myapp

"myapp" is publish on C:\publish\app\myapp with it's own appsettings.json

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();

var env = builder.Environment;
builder.Configuration
    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
    .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true)
    .AddEnvironmentVariables();

I want to add an another json file based on actual url :

if (HttpContext.Request.Host.Host == "local.domain1.com")
{
    // Load appsettings in C:\publish\shared\domain1\appsettings.json
}
else if (HttpContext.Request.Host.Host == "local.domain2.com")
{
    // Load appsettings in C:\publish\shared\domain2\appsettings.json
}

But in Program.cs, i don't have access to HttpContext. If I create a custom middlware, i can't call builder.Configuration.AddJsonFile(...)

How to do ?

Thank you

c#

.net-6.0

httpcontext

appsettings

0 Answers

Your Answer

Accepted video resources