Parameter value "expires" http header programmatically in asp.net

On an ASP.NET site, I would like to add the “Expires” header to some static files, so I added a configuration clientCachelike this for the folder where these files are located:

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

If possible, I would like to programmatically calculate the value httpExpiresto set it, for example, until the last file update + 24 hours.

Is there a way to configure the cache control to get the value httpExpiresby calling the method?

If not, what are the alternatives? I was thinking of writing a custom HTTP handler, but maybe there is a simpler solution ...

EDIT: Please note that these are static files, therefore they are not served by the regular asp.net page handler.

+5
2

Response.Cache, .

.

Public (clientside + proxies) . , .

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

( ASP.NET , ?)

, Response.Headers, .

ASP.NET(aspx, asmx) , , , HttpContext.

+7

@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() {
    }

}

- (IIS 7):

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

All Articles