繁体   English   中英

在asp.net中以编程方式设置“expires”http头的值

[英]Setting value of “expires” http header programmatically in asp.net

在ASP.NET站点中,我想为某些静态文件添加“Expires”标头,因此我为这些文件所在的文件夹添加了这样的clientCache配置:

<system.webServer>
  <staticContent>
    <clientCache cacheControlMode="UseExpires" httpExpires="Wed, 13 Feb 2013 08:00:00 GMT" />
  </staticContent>

如果可能的话,我想以编程方式计算httpExpires的值,例如将其设置为文件上次更新的时间+ 24小时。

有没有办法通过调用方法来配置缓存控件以获取httpExpires的值?

如果没有,有哪些替代方案? 我想过编写一个自定义的http处理程序,但也许有一个更简单的解决方案......

编辑 :请注意这些是静态文件,因此它们不是由常规的asp.net页面处理程序提供的。

您可以使用Response.Cache以编程方式设置缓存。

这是一个很好看的教程。

基本上,您希望将缓存策略设置为Public (clientside + proxies)并设置到期标头。 有些方法的名称相当笨拙,但这个方法很容易。

HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Public);
HttpContext.Current.Response.Cache.SetExpires(yourCalculatedDateTime);

(ASP.NET设计者不喜欢Demeter法,他们呢?)

或者,您可以通过Response.Headers集合访问heareds,您可以在其中明确更改它们。

这两种方式都可以在所有ASP.NET处理程序(aspx,asmx)中访问,并且可以在任何可以访问当前HttpContext的地方进行更改。

感谢@HonzaBrestan,他让我按照HTTP模块的想法走上正轨,我想出了一个像这样的解决方案,我想分享它,以防它对其他人有用。

using System;
using System.Collections.Generic;
using System.IO;
using System.Web;

public class ExpirationModule : IHttpModule {

    HttpApplication _context;

    #region static initialization for this example - this should be a config section

    static Dictionary<string, TimeSpan> ExpirationTimes;
    static TimeSpan DefaultExpiration = TimeSpan.FromMinutes(15);
    static CrlExpirationModule() {       
        ExpirationTimes = new Dictionary<string, TimeSpan>();
        ExpirationTimes["~/SOMEFOLDER/SOMEFILE.XYZ"] = TimeSpan.Parse("0.0:30");
        ExpirationTimes["~/ANOTHERFOLDER/ANOTHERFILE.XYZ"] = TimeSpan.Parse("1.1:00");
    }

    #endregion

    public void Init(HttpApplication context) {
        _context = context;
        _context.EndRequest += ContextEndRequest;
    }

    void ContextEndRequest(object sender, EventArgs e) {
        // don't use Path as it contains the application name
        string requestPath = _context.Request.AppRelativeCurrentExecutionFilePath;
        string expirationTimesKey = requestPath.ToUpperInvariant();
        if (!ExpirationTimes.ContainsKey(expirationTimesKey)) {
            // not a file we manage
            return;
        }
        string physicalPath = _context.Request.PhysicalPath;
        if (!File.Exists(physicalPath)) {
            // we do nothing and let IIS return a regular 404 response
            return;
        }
        FileInfo fileInfo = new FileInfo(physicalPath);
        DateTime expirationTime = fileInfo.LastWriteTimeUtc.Add(ExpirationTimes[expirationTimesKey]);
        if (expirationTime <= DateTime.UtcNow) {
            expirationTime = DateTime.UtcNow.Add(DefaultExpiration);
        }
        _context.Response.Cache.SetExpires(expirationTime);
    }

    public void Dispose() {
    }

}

然后,您需要在Web配置(IIS 7)中添加模块:

<system.webServer>
  <modules>
    <add name="ExpirationModule" type="ExpirationModule"/>
  </modules>
</system.webServer>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM