Asp.net-Mvc

如何在 ASP.NET Core 中啟用 ClientCache

  • December 5, 2017

在 ASP.net 4.5 中,我們曾經能夠通過在 web.config 中添加“ClientCache”來啟用靜態資源上的過期標頭(反過來,啟用瀏覽器記憶體),例如:

<staticcontent>
 <clientcache cachecontrolmode="UseMaxAge" cachecontrolmaxage="365.00:00:00" />
</staticcontent>

如http://madskristensen.net/post/cache-busting-in-aspnet中所引用

當我們沒有 web.config 和 Startup.cs 時,我們現在如何在 ASP.net 5 中執行此操作?

在 Startup.cs > Configure(IApplicationBuilder applicationBuilder, …..)

applicationBuilder.UseStaticFiles(new StaticFileOptions
{
    OnPrepareResponse = context => 
    context.Context.Response.Headers.Add("Cache-Control", "public, max-age=2592000")
});

如果您使用的是 MVC,則可以在您的操作上使用ResponseCacheAttribute來設置客戶端記憶體標頭。您還可以使用ResponseCacheFilter

引用自:https://stackoverflow.com/questions/32138286