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

Ant*_*eev 9 asp.net-mvc caching asp.net-mvc-routing mvc-mini-profiler

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

在移动到Assets文件夹之前和之后

我知道ContentASP.NET MVC约定不需要该文件夹,但为什么会发生这种情况?被Content莫名其妙地治疗尤其是人(如ASP.NET,IISExpress等)?以及如何强制缓存包含静态资源的其他文件夹?

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

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

MiniProfiler.Settings.IgnoredPaths = MiniProfiler.Settings.IgnoredPaths
    .Concat(new [] { "/assets/" })
    .ToArray();
Run Code Online (Sandbox Code Playgroud)

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

tug*_*erk 5

这是一种奇怪的行为.但是,将以下代码放在web.config文件中,该文件位于应用程序的根目录下:

  <system.webServer>
    <staticContent>
      <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="30.00:00:00" />
    </staticContent>
  </system.webServer>
Run Code Online (Sandbox Code Playgroud)

此代码附加必要的响应标头,以便浏览器缓存起作用.你可以调整当然的时间.有关详细信息,请参阅:http://www.iis.net/ConfigReference/system.webServer/staticContent/clientCache


Dav*_*und 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; }
Run Code Online (Sandbox Code Playgroud)

来源.

据推测,当你通过Cassini为它们提供服务时,图像从未被缓存,因为Cassini在这方面非常糟糕(例如,将png文件作为application/octet-stream传递),但是MvcMiniProfiler手动隐藏了这个问题.