在IIS中托管GWT Webapp

在IIS中托管GWT Webapp,iis,gwt,iis-7,html5-appcache,no-cache,Iis,Gwt,Iis 7,Html5 Appcache,No Cache,我目前正试图通过Web.config配置ASP.net Web应用程序,以便在特定文件夹中托管GWT WebApp。我已经在system.Webserver/staticContent节中成功地为.manifest文件扩展名配置了mimeMap,但是,我仍然使用clientCache。我想添加一个缓存规则,以便为带有“.nocache.”的文件提供以下标题: "Expires", "Sat, 21 Jan 2012 12:12:02 GMT" (today -1); "Pragma", "no-

我目前正试图通过Web.config配置ASP.net Web应用程序,以便在特定文件夹中托管GWT WebApp。我已经在system.Webserver/staticContent节中成功地为.manifest文件扩展名配置了mimeMap,但是,我仍然使用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+中做到这一点吗?

我最终创建了一个自定义httphandler来处理对path.nocache的所有请求。使用类似于此处所述的解决方案:


我最终创建了一个自定义httphandler来处理对path.nocache的所有请求。使用类似于此处所述的解决方案:


在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>

在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>

  • 在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 */
                }
    
            }
        }
    }
    
    使用系统;
    使用System.Web;
    使用System.Text.RegularExpressions;
    名称空间YourNamespace
    {
    /// 
    ///类别GwtCacheHttpModuleImpl
    /// 
    ///PurMeMeTele MeTr.Enter CalpUnEnter
    /// 
    公共类GwtCacheHttpModuleImpl:IHttpModule
    {
    私有HttpApplication_上下文;
    私有静态字符串GWT|U文件扩展名|REGEX|u String=“\\”(js|html | png | bmp | jpg | gif | htm | css | ttf | svg | woff | txt)$;
    私有静态正则表达式GWT_CACHE_或\u NO_CACHE_FILE_Regex=new Regex(“.\\\”(NO|)CACHE“+GWT_FILE_EXTENSIONS_Regex_STRING);
    私有静态正则表达式GWT\U缓存\U文件\U正则表达式=新正则表达式(“.\\.CACHE”+GWT\U文件扩展名\U正则表达式字符串);
    #区域IHttpModule Membres
    公共空间处置()
    {
    _上下文=空;
    }
    公共void Init(HttpApplication上下文)
    {
    context.PreSendRequestHeaders+=context\u PreSendRequestHeaders;
    _上下文=上下文;
    }
    #端区
    私有void上下文\u PreSendRequestHeaders(对象发送方,事件参数e)
    {
    int responseStatusCode=\u context.Response.StatusCode;
    开关(响应代码)
    {
    案例200:
    案例304:
    //Réponse géRée
    打破
    违约:
    //Réponse non géRée
    返回;
    }/*结束..开关*/
    字符串requestPath=\u context.Request.Path;
    如果(!GWT_CACHE_或_NO_CACHE_FILE_REGEX.IsMatch(requestPath))
    {
    //菲希尔非格雷斯酒店
    返回;
    }
    HttpCachePolicy cachePolicy=\u context.Response.Cache;
    if(GWT_CACHE_FILE_REGEX.IsMatch(requestPath))
    {
    //菲希耶·梅特瑞酒店
    cachePolicy.SetExpires(DateTime.UtcNow.Add(TimeSpan.FromDays(365));/*现在加上1年*/
    }
    其他的
    {
    //在高速缓存中显示一条消息
    cachePolicy.SetExpires(DateTime.UtcNow);/*ExpiresDefault“立即”*/
    cachePolicy.SetMaxAge(TimeSpan.Zero);/*max age=0*/
    cachePolicy.SetCacheability(HttpCacheability.Public);/*缓存控制公共*/
    cachePolicy.SetRevalidation(HttpCacheRevalidation.AllCaches);/*必须重新验证*/
    }
    }
    }
    }
    
  • 在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\u isapi.dll

    扩展名:.js

    限制为:GET,HEAD

    b) Handle.html扩展名

    可执行文件:c:\windows\microsoft.net\framework\v2.0.50727\aspnet\u isapi.dll

    扩展名:.html

    限制为:GET,HEAD

    参考:

  • 在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 */
                }
    
            }
        }
    }
    
    使用系统;
    使用System.Web;
    使用System.Text.RegularExpressions;
    名称空间YourNamespace
    {
    /// 
    ///类别GwtCacheHttpModuleImpl
    /// 
    ///PurMeMeTele MeTr.Enter CalpUnEnter
    /// 
    公共类GwtCacheHttpModuleImpl:IHttpModule
    {
    私有HttpApplication_上下文;
    私有静态字符串GWT|U文件扩展名|REGEX|u String=“\\”(js|html | png | bmp | jpg | gif | htm | css | ttf | svg | woff | txt)$;
    私有静态正则表达式GWT\U缓存\U或\U NO\C