![](/img/trans.png)
[英]C# Owin Security Providers returning private IP for Request.Host instead of domain
[英].NET 7.0 Change the webrootpath at runtime based on the Request.Host - How to get the host in webapplication builder
我為每個子域組織了 wwwroot 文件夾,如下所示。 該應用程序基於 .NET 7.0 並希望使用 webroot 中不同文件夾中的 StaticFiles
wwwroot
-- folder1.abc.com
--index.html
--index.js
-- folder2.abc.com
--index.html
--index.js
-- folder3.bcd.com
--index.html
--index.js
-- etc
folder1.abc.com 是 static 文件所在的文件夾名稱,如 index.html 並且正在為網站 folder1.abc.com 提供服務
我在 asp.net .net 7.0 項目中沒有任何控制器,除了 program.cs 和 appsettings.json,可能是 hostsettings.json
現在如何為子域https://folder1.abc.com提供來自 folder1.abc.com 的頁面
我希望從請求中獲取 httpcontext 和 Request.Host 並提供該文件夾中的文件。
我正在使用下面的
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
Args = args,
ApplicationName = typeof(Program).Assembly.FullName,
ContentRootPath = Directory.GetCurrentDirectory(),
WebRootPath = "initialdefaultfolder"
});
var app = builder.Build();
app.UseDefaultFiles();
app.UseStaticFiles();
app.Run();
================================
似乎無法動態地從請求中獲取主機名並提供該文件夾中的文件。 我想使用 app.UseStaticFiles()
並且還想在運行時動態地將 webrootpath 設置為
WebRootPath = Httpscontext.Request.Host.Host
在構建器之前或在應用程序中
但我不知道如何在 Program.cs 中獲取 HttpContext
我們如何在運行時根據請求主機名動態設置 webrootpath?
任何幫助表示贊賞。
試過類似的東西
`//app.Use(async (httpContext, next) =>
//{
// try
// {
// builder.WebHost.UseWebRoot("wwwroot/" + httpContext.Request.Host.Host);
// await next(httpContext);
// }
// finally
// {
// await next(httpContext);
// }
//});`
我似乎沒有得到它的工作。
您可以嘗試使用IApplicationBuilder.UseWhen
有條件地提供 static 個文件,如下所示:
var domains = new string[] { ... }; // or dictionary from domain to relative path
foreach (var domain in domains)
{
app.UseWhen(
ctx => ctx.Request.Host.Host.Contains(domain, StringComparison.OrdinalIgnoreCase), // match the domain
app => app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(Path.Combine(builder.Environment.WebRootPath, domain)) // path to files
}));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.