Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# “的设定值”;“到期”;asp.net中的http头以编程方式_C#_Asp.net_Caching_Http Headers - Fatal编程技术网

C# “的设定值”;“到期”;asp.net中的http头以编程方式

C# “的设定值”;“到期”;asp.net中的http头以编程方式,c#,asp.net,caching,http-headers,C#,Asp.net,Caching,Http Headers,在ASP.NET站点中,我想向某些静态文件添加一个“Expires”头,因此我为这些文件所在的文件夹添加了一个clientCache配置,如下所示: <system.webServer> <staticContent> <clientCache cacheControlMode="UseExpires" httpExpires="Wed, 13 Feb 2013 08:00:00 GMT" /> </staticContent>

在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页面处理程序不提供这些文件。

您可以使用以编程方式设置缓存

一个好看的教程

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

HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Public);
HttpContext.Current.Response.Cache.SetExpires(yourCalculatedDateTime);
(ASP.NET设计师不太喜欢德米特定律,是吗?)

或者,您可以通过
Response.Headers
collection访问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() {
    }

}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Web;
公共类过期模块:IHttpModule{
HttpApplication\u上下文;
#本例中的区域静态初始化-这应该是一个配置部分
静态字典过期时间;
静态TimeSpan DefaultExpiration=TimeSpan.FromMinutes(15);
静态CrlExpirationModule(){
ExpirationTimes=新字典();
ExpirationTimes[“~/SOMEFOLDER/SOMEFILE.XYZ”]=TimeSpan.Parse(“0.0:30”);
ExpirationTimes[“~/ANOTHERFOLDER/ANOTHERFILE.XYZ”]=TimeSpan.Parse(“1.1:00”);
}
#端区
公共void Init(HttpApplication上下文){
_上下文=上下文;
_context.EndRequest+=ContextEndRequest;
}
void ContextEndRequest(对象发送方,事件参数e){
//不要使用路径,因为它包含应用程序名称
字符串requestPath=\u context.Request.AppRelativeCurrentExecutionFilePath;
string expirationTimesKey=requestPath.ToUpperInvariant();
如果(!ExpirationTimes.ContainsKey(expirationTimesKey)){
//不是我们管理的文件
返回;
}
字符串physicalPath=\u context.Request.physicalPath;
如果(!File.Exists(physicalPath)){
//我们什么也不做,让IIS返回一个常规的404响应
返回;
}
FileInfo FileInfo=新文件信息(物理路径);
DateTime expirationTime=fileInfo.LastWriteTimeUtc.Add(ExpirationTimes[ExpirationTimeKey]);

if(expirationTime)谢谢,但是这些是静态文件,说它们有一个'xyz'扩展名,所以它们不是由通常的asp.net处理程序提供的。我必须自己编写吗?哦,对不起,你的问题中没有提到。在这种情况下,你应该寻找一个。如果是这样,我会更新我的答案
<system.webServer>
  <modules>
    <add name="ExpirationModule" type="ExpirationModule"/>
  </modules>
</system.webServer>