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

是的,谢谢,它绝对有效。 –

相关问题