Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 基于ASP.NET Core for SPA中的非路由或域确定静态文件_C#_Authentication_Asp.net Core_Routing_Single Page Application - Fatal编程技术网

C# 基于ASP.NET Core for SPA中的非路由或域确定静态文件

C# 基于ASP.NET Core for SPA中的非路由或域确定静态文件,c#,authentication,asp.net-core,routing,single-page-application,C#,Authentication,Asp.net Core,Routing,Single Page Application,是否可以在ASP.NET Core 2/3+中基于路由返回不同的静态文件 例如: app.domain.com将为公共SPA(ReactJS/VueJS)呈现一些index.html admin.domain.com将为私人身份验证SPA(角度)呈现一些其他index.html www.domain.com将为公共登录页呈现第三个index.html 我在文档中找不到如何根据域返回不同的内容,您可以尝试更改usesticfiles中的内容 app.UseStaticFiles(new Stati

是否可以在ASP.NET Core 2/3+中基于路由返回不同的静态文件

例如:

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

admin.domain.com
将为私人身份验证SPA(角度)呈现一些其他
index.html

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


我在文档中找不到如何根据域返回不同的内容,您可以尝试更改
usesticfiles
中的内容

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。

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

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 


}
公共类启动
{
public void配置服务(IServiceCollection服务)
{
services.AddOptions()
.配置(
委托(StaticFileOptions选项、IHttpContextAccessor httpContext、IWebHostEnvironment环境)
{
options.FileProvider=新的ClientAppFileProvider(httpContext,env);
}
);
}
公共void配置(IApplicationBuilder应用程序)
{
app.UseStaticFiles();
}
}
公共类ClientAppFileProvider:IFileProvider
{
专用只读IHttpContextAccessor\u httpContextAccessor;
私人只读IWebHostEnvironment;
公共客户端应用文件提供程序(IHttpContextAccessor httpContextAccessor,IWebHostEnvironment Environment)
{
_httpContextAccessor=httpContextAccessor;
_env=env;
}
公共IDirectoryContents GetDirectoryContents(字符串子路径)
{
返回_env.WebRootFileProvider.GetDirectoryContents(子路径);
}//结束函数GetDirectoryContents
公共IFileInfo GetFileInfo(字符串子路径)
{
字符串host=\u 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);
}
//返回_env.ContentRootFileProvider.GetFileInfo(子路径);
返回_env.WebRootFileProvider.GetFileInfo(子路径);
}
公共IChangeToken监视(字符串筛选器)
{
返回_env.WebRootFileProvider.Watch(过滤器);
}//结束函数监视
}

好主意!非常感谢你的代码!虽然它只完成了一半,还没有编译,但我有了这个想法,并设法让它开始工作。作为公共服务,我修复了示例代码中的错误。现在编译。此外,Path.Combine在这种情况下也不起作用。相反,字符串subpath2=subpath.StartsWith(“/”)吗?子路径:“/”+子路径;子路径2=“/”+主机+子路径2;