2015-03-25 94 views
7

我正在使用軟件包Microsoft.AspNet.StaticFiles並在Startup.cs中將其配置爲app.UseStaticFiles()。我如何更改交付文件的標題?我想設置緩存過期等圖像,CSS和JS。在Asp.net Core中更改靜態文件的標題

+1

[使用ASP.NET Core永遠緩存靜態資源](https://www.softfluent.com/blog/dev/2017/01/08/Caching-static-resources-forever-with-ASP-Core) – meziantou 2017-02-28 10:04:07

回答

10

您可以使用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或任何你想要的。

2

你必須寫一箇中間件要做到這一點,我已經是在ChatLe.HttpUtility項目中刪除我的github頭https://github.com/aguacongas/chatle
看樣品,這是一個有點棘手。你可以看看這個問題還有:

How to do remove some httpresponse headers on each response like Server and ETag?

但是這不會IIS下工作,因爲IIS管理靜態文件本身。它僅適用於獨立應用程序,如kestrelfirefly

+0

你的意思是所有這三個在IIS下無法工作?或者其中一個答案,具體是什麼? – 2016-01-11 15:31:16

1

在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> 
+0

如果網站託管.NET核心模塊(例如OP),那麼這是行不通的,因爲.NET核心不支持'web.config'。 .NET Core的工作方式基本上是運行他們自己的Web服務器Kestrel,它隱藏在IIS + HTTP模塊(又名ANCM,又名ASP.NET核心模塊)之後。 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/aspnet-core-module – 2017-02-01 06:46:18

1

如果你正在尋找一個解決方案,允許您爲每個環境(開發,生產&更多),這也是在的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核心文檔:

0

基於上述喬希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"); 
      } 
      } 
     }); 
相關問題