Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

And still, what is the magic of ASP.NET MVC Content folder?

I've just moved my resource files (javascript, css, images) from Content folder to custom Assets folder. And I've noticed a strange behavior - these files are not longer cached by browser and MvcMiniProfiler shows separate request for each resource located in Assets folder:

Before moving to Assets folder and after

I know that Content folder isn't required by ASP.NET MVC convention, but why this happens? Is Content treated somehow especially by anyone (e.g. ASP.NET, IISExpress, etc.)? And how to force caching for other folders that contain static resources?

EDIT: Oh, it appears to be not an ASP.NET MVC odd behavior, but just the standard behavior of MvcMiniProfiler. Currently I'm checking that...

EDIT: Yea, there is no problem with ASP.NET MVC, it's just a default configuration of MvcMiniProfiler to ignore only these paths: "/mini-profiler-", "/content/", "/scripts/", "/favicon.ico". And these defaults can be easily extended:

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

Sometimes it's a good idea to read documentation before using something ;)

like image 712
Anton Moiseev Avatar asked Nov 17 '11 09:11

Anton Moiseev


2 Answers

As you're indicating in your update, this appears to be a feature of 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.

Presumably, the images were never cached when you were serving them through Cassini, because Cassini is terrible at that (passing png files as application/octet-stream, for instance), but the issue was manually hidden from your view by MvcMiniProfiler.

like image 76
David Hedlund Avatar answered Oct 06 '22 20:10

David Hedlund


This is a strange behavior. However, put the following code inside your web.config file which is under the root of your app:

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

This code appends the necessary response headers in order for browser caching to work. You can tweak the time of course. For further info please refer : http://www.iis.net/ConfigReference/system.webServer/staticContent/clientCache

like image 32
tugberk Avatar answered Oct 06 '22 21:10

tugberk