asp.net-core-mvcvirtualhostweb-optimization

Serving multiple sites from the same wwwwroot


I have an ASP.NET Core 5 MVC application which serves multiple sites using the ASP.NET Core Web Optimizer; e.g.,

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

All sites have same wwwwroot directory.

I tried code below to provide a single wwwwroot in my startup.cs and it worked. However, Guru wrote that every middleware should be called only once.

How do I serve multiple sites using the same wwwroot directory?

In my startup.cs:

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {  
        app.UseWebOptimizer();
        app.UseWebOptimizer(env, new FileProviderOptions[] { new FileProviderOptions()
                {
                    RequestPath = new PathString("/store1"),
                    FileProvider = env.WebRootFileProvider
                }
                });
        app.UseWebOptimizer(env, new FileProviderOptions[] { new FileProviderOptions()
                {
                    RequestPath = new PathString("/store2"),
                    FileProvider = env.WebRootFileProvider
                }
                });
        app.UseStaticFiles();
        app.UseStaticFiles(new StaticFileOptions
        {
            RequestPath = new PathString("/store1")
        });
        app.UseStaticFiles(new StaticFileOptions
        {
            RequestPath = new PathString("/store2")
        });
    }

Solution

  • Are these sites different and independent projects? Or are they simply different routes under your project? If they're routes, you should simply change you Routing config inside your project. If they're independent sites, you should consider just setting up multiple applications under the root app in your web server and then publish each project to its application. For IIS there is this Microsoft guide on how to do it: https://learn.microsoft.com/en-us/iis/configuration/system.applicationhost/sites/site/application/