ASP.NET Core、.NET 6>基于 HttpContext / 当前 URL 覆盖应用程序设置
在我的 ASPNET Core 应用程序中,如何根据当前 url 重载/覆盖应用程序设置?
我实际上有一个应用程序“我的应用程序”,在 .NET 6 中。 我在 IIS 中托管此应用程序2次:
http://local.domain1.com /myapp
http://local.domain2.com/myapp
“myapp”已发布在C:\publish\app\myapp 及其自己的 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();
我想根据实际 url 添加另一个 json 文件:
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
}
但在 Program.cs 中,我无权访问 HttpContext。 如果我创建自定义中间件,我无法调用 builder.Configuration.AddJsonFile(...)
怎么办?
谢谢
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(quot;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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论