ファイルのタイムスタンプは IIS で自動的にチェックされ、ブラウザーは常にタイムスタンプに基づいて更新されたファイルをサーバーに要求するため、.nocache. ファイルは IIS で特別なものを必要としません。
ただし、ブラウザに .cache をキャッシュさせたい場合は. ファイルの場合、次の HttpModule は、.cache.js または .cache.html (または任意の拡張子) で終わるファイルのキャッシュ有効期限を今から 30 日後に設定します。ブラウザーは、これらのファイルの更新されたバージョンを要求することさえしません。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace CacheModulePlayground
{
public class CacheModule : IHttpModule
{
private HttpApplication _context;
public void Init(HttpApplication context)
{
_context = context;
context.PreSendRequestHeaders += context_PreSendRequestHeaders;
}
void context_PreSendRequestHeaders(object sender, EventArgs e)
{
if (_context.Response.StatusCode == 200 || _context.Response.StatusCode == 304)
{
var path = _context.Request.Path;
var dotPos = path.LastIndexOf('.');
if (dotPos > 5)
{
var preExt = path.Substring(dotPos - 6, 7);
if (preExt == ".cache.")
{
_context.Response.Cache.SetExpires(DateTime.UtcNow.Add(TimeSpan.FromDays(30)));
}
}
}
}
public void Dispose()
{
_context = null;
}
}
}
このための web.config は次のとおりです。
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<system.webServer>
<modules>
<add name="cacheExtension" type="CacheModulePlayground.CacheModule"/>
</modules>
</system.webServer>
</configuration>