How use weboptimizer to with multiple virtual directories and single wwwroot

906 views Asked by At

ASP.NET 5 MVC Core application serves multiple sites using LigerShark WebOptimizer ( https://github.com/ligershark/WebOptimizer )

https://example.com/store1
https://example.com/store2
https://example2.com

All those sites should served from wwwroot directory containing same files for those urls. Sites are defined in hosts.json file:

{
  "EevaHosts": {
    "example.com/store1": {}
    "example.com/store2": {}
    "example2.com": {}
  }
}

I tried code below to force WebOptimizer to use same wwwwroot directory for every site in Debian Linux but got exception for https://example.com/store1/site.js

No files found matching "/store1/js/site.js" exist in "/var/www/appbasedir/wwwroot/"

How to force web optimizer to use same wwwwroot directory for all sites ? If Weboptimizer middleware is removed, static files are serverd properly.

In StartUp.cs:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{ ...

    var eevakonf = new ConfigurationBuilder().AddJsonFile("hosts.json").Build();

    foreach (var host1 in eevakonf.GetSection("EevaHosts").GetChildren())
    {
        if (!host1.Key.Contains("/"))
            app.UseWebOptimizer();
        else
            app.UseWebOptimizer(env, new FileProviderOptions[] { new FileProviderOptions()
            {
                // example.com/store1 -> /store1
                RequestPath = new PathString(RequestPathExtract(host1)),
                FileProvider = env.WebRootFileProvider
            }
            });
    }

    // Using single call causes the same exception:
    //HashSet<FileProviderOptions> fp = new();
    //foreach (var host1 in eevakonf.GetSection("EevaHosts").GetChildren())
    //{
    //    if (host1.Key.Contains("/"))
    //        fp.Add(new FileProviderOptions() {
    //            RequestPath = new PathString(RequestPathExtract(host1)) ,
    //            FileProvider = env.WebRootFileProvider
    //        });
    //}
    //app.UseWebOptimizer(env, fp.ToArray());

    foreach (var host in eevakonf.GetSection("EevaHosts").GetChildren())
    {
        if (!host.Key.Contains("/"))
            app.UseStaticFiles();
        else
        {
            app.UseStaticFiles(new StaticFileOptions
            {
                RequestPath = new PathString(RequestPathExtract(host))
            });
        }
    }
  }


    static string RequestPathExtract(IConfigurationSection host)
    {
        return "/" + StrExtract(host.Key, "/");
    }

 static string StrExtract(string cSearchExpression, string cBeginDelim)
{
    int nbpos = At(cBeginDelim, cSearchExpression);
    return cSearchExpression[(nbpos + cBeginDelim.Length - 1)..];
}
0

There are 0 answers