0

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.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • Why would you wanna do something like this? This is quite confusing and unclear. Are you trying to run multiple SPAs within the same .NET app? – Mosia Thabo Jan 18 '23 at 18:45
  • Yes. Trying to get Multiple SPA serving from different folders for different sub domains – stack overflow Jan 18 '23 at 18:57
  • Your entire architecture sounds wrong. Why use 1 domain as storage for applications that are ran from subdomains? remember every domain represents a different folder system within the same server package. Unless if you're trying to use the main domain as some sort of cdn for your applications from subdomains? – Mosia Thabo Jan 18 '23 at 19:00
  • There is a need for this to work. How can we get the webrootpath changed at runtime based on the Request.Host. That is the question am trying to solve. Not concerned about the architecture is right or wrong. There is a purpose to it. – stack overflow Jan 18 '23 at 19:02
  • Check this out [Forking the pipeline - adding tenant-specific files with SaasKit in ASP.NET Core](https://andrewlock.net/forking-the-pipeline-adding-tenant-specific-files-with-saaskit-in-asp-net-core/) – Dmitry Pavlov Jan 18 '23 at 20:05
  • This link shows raw html. The link is not rendering as html – stack overflow Jan 18 '23 at 20:23

1 Answers1

0

You can try using IApplicationBuilder.UseWhen to conditionally serve static files, something along this lines:

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
        }));
}
Guru Stron
  • 102,774
  • 10
  • 95
  • 132