1

ASP.NET 5 MVC Core 应用程序使用 LigerShark WebOptimizer ( https://github.com/ligershark/WebOptimizer )为多个站点提供服务

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

所有这些站点都应从包含与这些 url 相同的文件的 wwwroot 目录提供。站点在 hosts.json 文件中定义:

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

我尝试了下面的代码来强制 WebOptimizer 为 Debian Linux 中的每个站点使用相同的 wwwwroot 目录,但https://example.com/store1/site.js出现异常

在“/var/www/appbasedir/wwwroot/”中找不到匹配“/store1/js/site.js”的文件

如何强制网络优化器对所有站点使用相同的 wwwwroot 目录?如果 Weboptimizer 中间件被移除,静态文件会被正确地服务。

在 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)..];
}
4

0 回答 0