加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 从Web API提供文件和路由

发布时间:2020-12-15 22:44:44 所属栏目:百科 来源:网络整理
导读:我们现在有一个现有的Web API项目,我们现在也想从root服务2个客户端(通过不同的子域访问).设置Web API以执行为两个网站提供服务文件和命中路由的功能的正确方法是什么? 我看到两个主要选择: 使用Kestrel作为文件服务器.我不确定如何配置Kestrel从root服务
我们现在有一个现有的Web API项目,我们现在也想从root服务2个客户端(通过不同的子域访问).设置Web API以执行为两个网站提供服务文件和命中路由的功能的正确方法是什么?

我看到两个主要选择:

>使用Kestrel作为文件服务器.我不确定如何配置Kestrel从root服务器来自两个站点(来自不同的子域).它似乎具有最小的配置暴露,并且这似乎不可扩展为基于子域服务2个站点.

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

host.Run();

此外,我目前无法将Kestrel与Web API集成,请参阅此StackOverflow question.
>从负责root的Web API控制器提供文件.这将读取URL并返回正确的index.html.从那里我不确定它将如何服务index.html引用的其他文件,如js / app.js,css / core.css或assets / *.可以使个别控制器处理这些,但这似乎很脆弱.

哪种方法适合?或者还有什么我还没有考虑过要实现这个目标吗?

解决方法

根据@ FedericoDipuma的评论,我最终使用了以下Startup.cs的OWIN:

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","max-age=0" });
                };

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读