在 Kentico Xperience 13 .NET Core 站点中将静态资产设置为可缓存

Set static assets as cacheable in Kentico Xperience 13 .NET Core site

我正在尝试在 .NET Core 站点中使用以下代码 运行 Kentico Xperience 13。

app.UseStaticFiles(new StaticFileOptions
{
    OnPrepareResponse = context =>
    {
        context.Context.Response.Headers[HeaderNames.CacheControl] = staticFileCacheHeaderValue;
    },
    HttpsCompression = HttpsCompressionMode.Compress
});

这有效,因为我在 wwwroot 下的静态文件设置了 cache-control header(& gzip 压缩)。

但是,使用此代码后,页面构建器和表单构建器脚本和样式将不再加载,出现 404。例如,在表单构建器中,它应该加载此文件:/Kentico/Scripts/builders/builder.css。以 /Kentico 或 /_content 开头的任何内容突然 404s。

无论我作为 StaticFileOptions 传递什么,都会发生这种情况 - 即使是简单的 new StaticFileOptions().

我试过在之前和之后调用 app.UseKentico(),这没有什么区别。如果我调用 app.UseStaticFiles() 以及上面的方法,则没有 Kentico 错误,但是 wwwroot 文件没有得到缓存 header 应用(它们也没有被压缩)。

我对 .NET Core 不是很熟悉,所以我不确定我是否遗漏了这方面的东西,或者 Kentico 是否还没有玩得很好。当然,非常感谢任何帮助!

有必要在给定的中间件中设置缓存控制:

app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions

{

    OnPrepareResponse = ctx => ctx.Context.Response.Headers.Append(HeaderNames.CacheControl, $"public, max-age={60 * 60 * 24 * 365}"),

    FileProvider = new PhysicalFileProvider(Path.Combine(Environment.ContentRootPath, @"Content")),

    RequestPath = new PathString("/Content")

});

或者如果我想将其设置为默认值app.UseStaticFiles(),则需要在 ConfigureServicesmethodStartup.cs 通过 services.Configure :

services.Configure<StaticFileOptions>(options =>
{
    options.OnPrepareResponse = ctx => ctx.Context.Response.Headers.Append(HeaderNames.CacheControl, $"public, max-age={60 * 60 * 24 * 365}");
});