我目前正在尝试通过Web.config配置ASP.net Web应用程序,以便在特定文件夹中托管GWT Web应用程序。我已经为系统中的.manifest文件扩展名配置了mimeMap。然而,在Webserver/staticContent部分,我被clientCache卡住了。我想添加一个缓存规则,这样带有".nocache."的文件就可以使用以下标头:
"Expires", "Sat, 21 Jan 2012 12:12:02 GMT" (today -1);
"Pragma", "no-cache"
"Cache-control", "no-cache, no-store, must-revalidate"
有人知道如何在IIS 7+中做到这一点吗?
文件时间戳在IIS中自动检查,浏览器总是根据时间戳请求服务器更新文件,因此.nocache.文件在IIS中不需要任何特殊的东西。
但是,如果您希望浏览器缓存.cache.files,那么以下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>
我最终创建了一个自定义的httphandler来处理对路径.nocache.的所有请求,使用了一个类似于此处描述的解决方案:
阻止脚本以编程方式缓存
-
在GwtCacheHttpModuleImpl.cs文件中创建HTTP模块类
using System; using System.Web; using System.Text.RegularExpressions; namespace YourNamespace { /// <summary> /// Classe GwtCacheHttpModuleImpl /// /// Permet de mettre en cache pour un an ou pas du tout les fichiers générés par GWT /// </summary> public class GwtCacheHttpModuleImpl : IHttpModule { private HttpApplication _context; private static String GWT_FILE_EXTENSIONS_REGEX_STRING = "\.(js|html|png|bmp|jpg|gif|htm|css|ttf|svg|woff|txt)$"; private static Regex GWT_CACHE_OR_NO_CACHE_FILE_REGEX = new Regex(".*\.(no|)cache" + GWT_FILE_EXTENSIONS_REGEX_STRING); private static Regex GWT_CACHE_FILE_REGEX = new Regex(".*\.cache" + GWT_FILE_EXTENSIONS_REGEX_STRING); #region IHttpModule Membres public void Dispose() { _context = null; } public void Init(HttpApplication context) { context.PreSendRequestHeaders += context_PreSendRequestHeaders; _context = context; } #endregion private void context_PreSendRequestHeaders(object sender, EventArgs e) { int responseStatusCode = _context.Response.StatusCode; switch (responseStatusCode) { case 200: case 304: // Réponse gérée break; default: // Réponse non gérée return; } /* end..switch */ String requestPath = _context.Request.Path; if (!GWT_CACHE_OR_NO_CACHE_FILE_REGEX.IsMatch(requestPath)) { // Fichier non géré return; } HttpCachePolicy cachePolicy = _context.Response.Cache; if (GWT_CACHE_FILE_REGEX.IsMatch(requestPath)) { // Fichier à mettre en cache cachePolicy.SetExpires(DateTime.UtcNow.Add(TimeSpan.FromDays(365))); /* now plus 1 year */ } else { // Fichier à ne pas mettre en cache cachePolicy.SetExpires(DateTime.UtcNow); /* ExpiresDefault "now" */ cachePolicy.SetMaxAge(TimeSpan.Zero); /* max-age=0 */ cachePolicy.SetCacheability(HttpCacheability.Public); /* Cache-Control public */ cachePolicy.SetRevalidation(HttpCacheRevalidation.AllCaches); /* must-revalidate */ } } } }
-
在Web.Config文件中引用您的HTTP模块:
-
通过ISAPI模块处理GWT文件扩展
您应该通过IIS UI(在我的案例中是IIS 5.x和.NET 3.5)配置您的应用程序。您可以添加其他GWT文件扩展名,如png、css。。。
a) Handle.js扩展
可执行文件:c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll
扩展:.js
限制为:GET,HEAD
b) Handle.html扩展
可执行文件:c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll
扩展:.html
限制为:GET,HEAD
参考:GWT Perfect Caching for Apache服务器