无法在asp.net核心中为SPA文件设置HTTP标头

问题描述 投票:0回答:1

在从命令行直接运行时(使用Kestrel)提供在ASP.Net核心2.2中作为SPA应用程序一部分的文件时,设置HTTP标头。

按照https://github.com/aspnet/AspNetCore/issues/3147#issuecomment-435617378的指示,我无法完成StaticFileOptions.OnPrepareResponse事件。标头不会被设置,甚至断点也不会被触发。

Statup.Configure()看起来像这样:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Error");
    }

    app.UseStaticFiles();
    app.UseSpaStaticFiles(new StaticFileOptions
    {
        OnPrepareResponse = ctx =>
        {
            ctx.Context.Response.Headers.Add("Hello", "World"); // Never triggers
        }
    });

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller}/{action=Index}/{id?}");
    });

    app.UseSpa(spa =>
    {
        spa.Options.SourcePath = "ClientApp";
        spa.Options.DefaultPageStaticFileOptions = new StaticFileOptions
        {
            OnPrepareResponse = ctx =>
            {
                ctx.Context.Response.Headers.Add("Hello", "World"); // Never triggers
            }
        };

        if (env.IsDevelopment())
        {
            spa.UseAngularCliServer(npmScript: "start");
        }
    });
}
asp.net-core single-page-application
1个回答
0
投票

事实证明,在运行angular dev服务器时,事件不会触发。这些线是罪魁祸首:

if (env.IsDevelopment())
{
    spa.UseAngularCliServer(npmScript: "start");
}

评论它或改变ASPNETCORE_ENVIRONMENT环境变量就可以了。

© www.soinside.com 2019 - 2024. All rights reserved.