【发布时间】:2015-05-29 03:47:32
【问题描述】:
我正在使用包Microsoft.AspNet.StaticFiles 并将其在Startup.cs 中配置为app.UseStaticFiles()。如何更改已交付文件的标题?我想为图像、css 和 js 设置缓存过期等。
【问题讨论】:
标签: asp.net-core asp.net-core-mvc
我正在使用包Microsoft.AspNet.StaticFiles 并将其在Startup.cs 中配置为app.UseStaticFiles()。如何更改已交付文件的标题?我想为图像、css 和 js 设置缓存过期等。
【问题讨论】:
标签: asp.net-core asp.net-core-mvc
您必须编写一个中间件来执行此操作,我有一个示例可以删除我的 github 上的标头 https://github.com/aguacongas/chatle
看看 ChatLe.HttpUtility 项目,有点棘手。你也可以看看这个问题:
How to do remove some httpresponse headers on each response like Server and ETag?
但是这在IIS 下不起作用,因为IIS 自己管理静态文件。它仅适用于独立应用程序,如 kestrel 或 firefly
【讨论】:
在 IIS 下,您可以将 web.config 文件添加到带有标头配置的 wwwroot 文件夹。一个控制所有文件的缓存头的例子:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<!-- Disable caching -->
<httpProtocol>
<customHeaders>
<add name="Cache-Control" value="no-cache" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
【讨论】:
web.config。 .NET Core 的工作方式本质上是运行自己的称为 Kestrel 的 Web 服务器,它隐藏在 IIS + HTTP 模块(又名 ANCM,又名 ASP.NET Core 模块)后面。 docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/…
您可以使用 StaticFileOptions,它包含一个事件处理程序,在静态文件的每个请求上都会调用该处理程序。
您的 Startup.cs 应该如下所示:
// Add static files to the request pipeline.
app.UseStaticFiles(new StaticFileOptions()
{
OnPrepareResponse = (context) =>
{
// Disable caching of all static files.
context.Context.Response.Headers["Cache-Control"] = "no-cache, no-store";
context.Context.Response.Headers["Pragma"] = "no-cache";
context.Context.Response.Headers["Expires"] = "-1";
}
});
当然,你可以修改上面的代码来检查内容类型,只修改 JS 或 CSS 或任何你想要的标题。
【讨论】:
"no-cache, no-store" 来提高性能,例如:context.Context.Response.Headers [HeaderNames.CacheControl] = "public, max-age=86400";(这里的 86400 是 24 小时 = 24*60*60 秒)。为了在更新文件时强制绕过缓存,我们还可以在<link>、<script> ...标签旁边使用asp-append-version="true"标签助手来添加一些基于文件哈希的自动生成的查询字符串,这将是自动更新。
如果您正在寻找一种解决方案,允许您为每个环境(开发、生产等)配置不同的行为,这也是在 web.config 文件中进行这些设置的意义所在您可以考虑以下方法,而不是对整个内容进行硬编码。
在 appsettings.json 文件中添加以下键/值部分:
"StaticFiles": {
"Headers": {
"Cache-Control": "no-cache, no-store",
"Pragma": "no-cache",
"Expires": "-1"
}
}
然后在 Startup.cs 文件的Configure 方法中相应地添加以下内容:
app.UseStaticFiles(new StaticFileOptions()
{
OnPrepareResponse = (context) =>
{
// Disable caching for all static files.
context.Context.Response.Headers["Cache-Control"] = Configuration["StaticFiles:Headers:Cache-Control"];
context.Context.Response.Headers["Pragma"] = Configuration["StaticFiles:Headers:Pragma"];
context.Context.Response.Headers["Expires"] = Configuration["StaticFiles:Headers:Expires"];
}
});
这将允许开发人员使用不同/多个/级联设置文件(appsettings.json、appsettings.production.json 等)定义不同的缓存设置 - 这可以使用旧的 web.config 配置模式来完成 -使用 ASP.NET Core 的新版本。
有关该主题的更多信息,我还建议阅读我的博客上的 this post 和/或官方 ASP.NET Core 文档中的这些精彩文章:
【讨论】:
根据上面 Josh Mouch 的回答,添加代码以确定它是否为 pdf 文件
Startup.cs:
app.UseStaticFiles(new StaticFileOptions
{
OnPrepareResponse = ctx =>
{
if(ctx.File.Name.ToLower().EndsWith(".pdf"))
{
ctx.Context.Response.Headers.Append("Cache-Control", "public,max-age=86400");
}
else
{
ctx.Context.Response.Headers.Append("Cache-Control", "public,max-age=31104000");
}
}
});
【讨论】: