I have wwwroot folder organized for each subdomain as below. The application is based on .NET 7.0 and want to Use StaticFiles from different folders in webroot
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 is the folder name where the static files are present like index.html and are being served for the website folder1.abc.com
I don't have any controllers in the asp.net .net 7.0 project except program.cs and appsettings.json and may be hostsettings.json
Now how do I serve pages from folder1.abc.com for the subdomain https://folder1.abc.com
I was hoping to get httpcontext and Request.Host from the Request and serve the files from that folder.
I am using the below
Program.cs
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();
================================
Nothing seems to work to dynamicall get the hostname from the request and serve the files from that folder. I want to use app.UseStaticFiles()
and also want to set the webrootpath dynamically at runtime as
WebRootPath = Httpscontext.Request.Host.Host
before the builder or in the app
But I do not know how to get the HttpContext in the Program.cs
How do we dynamically set the webrootpath based on the Request Hostname at run time?
Any help is appreciated.
Tried something like
`//app.Use(async (httpContext, next) =>
//{
// try
// {
// builder.WebHost.UseWebRoot("wwwroot/" + httpContext.Request.Host.Host);
// await next(httpContext);
// }
// finally
// {
// await next(httpContext);
// }
//});`
I don't seem to get it working.