Serving files and routes from the web API

We have an existing web API project, which we now want to serve 2 clients from root (access through different subdomains). What is the right way to configure the web API to perform file maintenance functions for two websites and routes?

I see two main options:

  • Use Kestrel as a file server. I'm not sure how to configure Kestrel to serve two root sites (coming from different subdomains). It seems that the minimal configuration is displayed as is, and it does not seem extensible to serve 2 sites based on a subdomain.

    var host = new WebHostBuilder()
       .UseKestrel()
       .UseWebRoot("wwwroot")
       .UseIISIntegration()
       .UseStartup<Startup>()
       .Build();
    
    host.Run();
    

    In addition, I now have problems with Kestrel integration with Web-API, see. This qaru.site/questions/314632 / ... .

  • -API, root. URL index.html. , , index.html, js/app.js, css/core.css assets/*. , .

? - , ?

+4
1

@FedericoDipuma, OWIN Startup.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Owin;
using Microsoft.Owin.FileSystems;
using Microsoft.Owin.StaticFiles;
using System.IO;

namespace SealingServer
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.MapWhen(ctx => ctx.Request.Headers.Get("Host").Equals("subdomain1.site.com"), app2 =>
            {
                var firstClientRoot = Path.Combine("./firstClient/");
                var firstClientFileSystem = new PhysicalFileSystem(firstClientRoot);

                var fileServerOptions = new FileServerOptions();
                fileServerOptions.EnableDefaultFiles = true;
                fileServerOptions.FileSystem = firstClientFileSystem;
                fileServerOptions.DefaultFilesOptions.DefaultFileNames = new[] {"home.html"};
                fileServerOptions.StaticFileOptions.OnPrepareResponse = staticFileResponseContext =>
                {
                    staticFileResponseContext.OwinContext.Response.Headers.Add("Cache-Control", new[] { "public", "max-age=0" });
                };

                app2.UseFileServer(fileServerOptions);
            });
            app.MapWhen(ctx => ctx.Request.Headers.Get("Host").Equals("subdomain2.site.com"), app2 =>  
            {
                var secondClientRoot = Path.Combine("./secondClient/");
                var secondClientFileSystem = new PhysicalFileSystem(secondClientRoot);

                var fileServerOptions = new FileServerOptions();
                fileServerOptions.EnableDefaultFiles = true;
                fileServerOptions.FileSystem = secondClientFileSystem;
                fileServerOptions.DefaultFilesOptions.DefaultFileNames = new[] { "home.html" };
                fileServerOptions.StaticFileOptions.OnPrepareResponse = staticFileResponseContext =>
                {
                    staticFileResponseContext.OwinContext.Response.Headers.Add("Cache-Control", new[] { "public", "max-age=0" });
                };

                app2.UseFileServer(fileServerOptions);
            });
        }
    }
}
+1

Source: https://habr.com/ru/post/1677938/


All Articles