8

我剛剛將Content文件夾中的資源文件(javascript,css,圖像)移至自定義Assets文件夾。而且我注意到一個奇怪的現象 - 這些文件不再由瀏覽器緩存和MvcMiniProfiler說明位於Assets夾中的每個資源單獨請求:還有,ASP.NET MVC Content文件夾的神奇之處是什麼?

Before moving to Assets folder and after

我知道Content文件夾不會通過ASP要求.NET MVC約定,但爲什麼會發生這種情況? Content是否被某些人(例如ASP.NET,IISExpress等)以某種方式對待?以及如何強制緩存包含靜態資源的其他文件夾?

編輯:哦,它似乎不是一個ASP.NET MVC奇怪的行爲,但只是MvcMiniProfiler的標準行爲。目前我檢查......

編輯:是啊,有一個與ASP.NET MVC沒有問題,它只是MvcMiniProfiler的default configuration只忽略這些路徑:"/mini-profiler-", "/content/", "/scripts/", "/favicon.ico"。而這些默認值可以很容易擴展:

MiniProfiler.Settings.IgnoredPaths = MiniProfiler.Settings.IgnoredPaths 
    .Concat(new [] { "/assets/" }) 
    .ToArray(); 

有時候這是一個好主意,使用的東西前閱讀文檔;)

+1

相似的問題 - [C#Mini MVC profiler:似乎顯示每個靜態資源的配置文件時間!](http://stackoverflow.com/questions/6648249/c-sharp-mini-mvc-profiler-appears-to -be-displays-profile-times-for-every-stat) –

回答

4

當你表明你的更新,這似乎是MvcMiniProfiler的一個特徵:

/// <summary> 
/// When <see cref="MiniProfiler.Start"/> is called, if the current request url contains any items in this property, 
/// no profiler will be instantiated and no results will be displayed. 
/// Default value is { "/mini-profiler-", "/content/", "/scripts/", "/favicon.ico" }. 
/// </summary> 
[DefaultValue(new string[] { "/mini-profiler-", "/content/", "/scripts/", "/favicon.ico" })] 
public static string[] IgnoredPaths { get; set; } 

Source

大概,當你通過卡西尼服務它們時,圖像從未被緩存,因爲卡西尼很糟糕(比如傳遞png文件作爲application/octet-stream),但是這個問題是通過手動方式隱藏的MvcMiniProfiler。

+0

謝謝,我們是對的:)!我在同一個文件中找到了答案,但是想知道這個功能是否記錄在某處,並且沒有找到任何文檔或博客文章,只是源代碼。 –

4

這是一個奇怪的行爲。然而,把你的web.config文件中下面的代碼是你的應用程序的根目錄下:

<system.webServer> 
    <staticContent> 
     <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="30.00:00:00" /> 
    </staticContent> 
    </system.webServer> 

此代碼追加必要的響應頭,以便瀏覽器緩存的工作。你當然可以調整時間。欲瞭解更多信息,請參閱:http://www.iis.net/ConfigReference/system.webServer/staticContent/clientCache

+0

是的,謝謝,它絕對有效。 –

相關問題