【问题标题】:And still, what is the magic of ASP.NET MVC Content folder?而且,ASP.NET MVC Content 文件夹的魔力是什么?
【发布时间】:2011-11-17 09:22:21
【问题描述】:

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

我知道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();

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

【问题讨论】:

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


【解决方案1】:

正如您在更新中指出的那样,这似乎是 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 从您的视图中手动隐藏了该问题.

【讨论】:

  • 谢谢,我们是对的 :)!我在同一个文件中找到了答案,但想知道这个功能是否记录在某个地方,并且没有找到任何文档或博客文章,只是源代码。
【解决方案2】:

这是一种奇怪的行为。但是,请将以下代码放入应用根目录下的 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

【讨论】:

    猜你喜欢
    • 2023-03-29
    • 2016-09-23
    • 1970-01-01
    • 2015-09-09
    • 2012-05-15
    • 2012-11-30
    • 2011-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多