繁体   English   中英

确定 Static 文件基于 ASP.NET Core for SPA 中的非路由或域

[英]Determine Static files based off route or domain in ASP.NET Core for SPA

是否可以根据 ASP.NET Core 2 / 3 + 中的路线返回不同的 static 文件?

例如:

app.domain.com将呈现一些index.html用于公共 SPA (ReactJS / VueJS)

admin.domain.com将呈现一些其他index.html用于私人认证的 SPA (Angular)

www.domain.com将呈现第三个index.html用于公共登录页面

我在文档https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-3.0中找不到如何

您可以随时尝试自定义文件提供程序:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddOptions<StaticFileOptions>()
            .Configure<IHttpContextAccessor, IWebHostEnvironment>(
                delegate(StaticFileOptions options, IHttpContextAccessor httpContext, IWebHostEnvironment env)
                {
                    options.FileProvider = new ClientAppFileProvider (httpContext, env);
                }
            );

    }

    public void Configure(IApplicationBuilder app)
    {
        app.UseStaticFiles();
    }
}

public class ClientAppFileProvider : IFileProvider
{
    private readonly IHttpContextAccessor _httpContextAccessor;
    private readonly IWebHostEnvironment _env;

    public ClientAppFileProvider(IHttpContextAccessor httpContextAccessor, IWebHostEnvironment env)
    {
        _httpContextAccessor = httpContextAccessor;
        _env = env;
    }

    public IDirectoryContents GetDirectoryContents(string subpath)
    {
        return _env.WebRootFileProvider.GetDirectoryContents(subpath); ;
    } // End Function GetDirectoryContents 


    public IFileInfo GetFileInfo(string subpath)
    {
        string host = _httpContextAccessor.HttpContext.Request.Host.Host;
        if (host.Equals("app.domain.com"))
        {
            subpath = Path.Combine("app", subpath);
        }
        else if (host.Equals("admin.domain.com"))
        {
            subpath = Path.Combine("admin", subpath);
        }
        else if (host.Equals("www.domain.com"))
        {
            subpath = Path.Combine("www", subpath);
        }

        // return _env.ContentRootFileProvider.GetFileInfo(subpath);
        return _env.WebRootFileProvider.GetFileInfo(subpath);
    }


    public IChangeToken Watch(string filter)
    {
        return _env.WebRootFileProvider.Watch(filter);
    } // End Function Watch 


}

要根据域返回不同的内容,您可以尝试更改UseStaticFiles中的内容,例如

app.UseStaticFiles(new StaticFileOptions
{
    OnPrepareResponse = async ctx =>
    {
        var request = ctx.Context.Request;
        string index = "";
        if (request.Host.Host.Equals("app.domain.com") && request.Path.Value.Contains("index.html"))
        {
            index = Path.Combine(Directory.GetCurrentDirectory(),
                "wwwroot", "PublicIndex.html");
        }
        else if (request.Host.Host.Equals("admin.domain.com") && request.Path.Value.Contains("index.html"))
        {
            index = Path.Combine(Directory.GetCurrentDirectory(),
                "wwwroot", "PrivateIndex.html");
        }
        if (!String.IsNullOrEmpty(index))
        {
            var fileBytes = File.ReadAllBytes(index);
            ctx.Context.Response.ContentLength = fileBytes.Length;
            using (var stream = ctx.Context.Response.Body)
            {
                await stream.WriteAsync(fileBytes, 0, fileBytes.Length);
                await stream.FlushAsync();
            }
        }
    }
});

确保wwwroot文件夹中有一个默认的 index.html。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM