而且,ASP.NET MVC Content文件夹的神奇之处是什么?

时间:2011-11-17 09:22:22

标签: asp.net-mvc caching asp.net-mvc-routing mvc-mini-profiler

我刚将资源文件(javascript,css,images)从Content文件夹移动到自定义Assets文件夹。我注意到一个奇怪的行为 - 这些文件不再被浏览器缓存,MvcMiniProfiler显示位于Assets文件夹中的每个资源的单独请求:

Before moving to Assets folder and after

我知道ASP.NET MVC约定不需要Content文件夹,但为什么会这样?是Content特别是由任何人(例如ASP.NET,IISExpress等)以某种方式对待?以及如何强制缓存包含静态资源的其他文件夹?

编辑:哦,它似乎不是ASP.NET MVC奇怪的行为,而只是MvcMiniProfiler的标准行为。目前我正在检查......

编辑:是的,ASP.NET MVC没有问题,只有default configuration的MvcMiniProfiler才会忽略这些路径:"/mini-profiler-", "/content/", "/scripts/", "/favicon.ico"。这些默认值可以很容易地扩展:

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

有时在使用之前阅读文档是个好主意;)

2 个答案:

答案 0 :(得分:5)

这是一种奇怪的行为。但是,将以下代码放在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

答案 1 :(得分:5)

正如您在更新中指出的那样,这似乎是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

据推测,当您通过Cassini提供图像时,图像从未被缓存,因为Cassini非常糟糕(例如,将png文件作为application / octet-stream传递),但是MvcMiniProfiler手动隐藏了该问题。