Setting component expires header via a HttpModule

Discussion in 'ASP .Net' started by helveticus, Sep 14, 2008.

  1. helveticus

    helveticus Guest

    I'm having difficulties setting the expires header for images and css
    files in my compression HttpModule. Compression is carried out by
    context_BeginRequest and works fine (code not shown). However, I
    can't set the expiration header for selected file extensions (ie. gif
    and css). Firebug' s YSL reports that these components do not have a
    far future Expires header.

    Question: How should I modify the code to set the expires header? TIA
    for any hints.

    Public Class CompressionModule
    Implements IHttpModule

    Public Sub Dispose() Implements System.Web.IHttpModule.Dispose
    End Sub

    Public Sub Init(ByVal context As System.Web.HttpApplication)
    Implements System.Web.IHttpModule.Init
    AddHandler context.BeginRequest, AddressOf
    context_BeginRequest ' Carries out compression
    AddHandler context.EndRequest, AddressOf
    context_EndRequest
    End Sub

    Public Sub context_EndRequest(ByVal sender As Object, ByVal e
    As EventArgs)
    Dim app As HttpApplication = CType(sender,
    HttpApplication)

    If app.Response.ContentType = "image/gif" Or
    app.Response.ContentType = "text/css" Then
    app.Response.ExpiresAbsolute =
    DateTime.Now.AddMinutes(5)

    app.Response.Cache.SetCacheability(HttpCacheability.Public)
    End If
    End Sub
    End Class
     
    helveticus, Sep 14, 2008
    #1
    1. Advertising

  2. helveticus

    bruce barker Guest

    gif & css (and other static files) are not mapped to asp.net by default.
    you will need to update the mapping in iis. there is a performance hit
    using asp.net to server static content. you can also tel iis to set the
    cache headers for certain extensions.

    -- bruce (sqlwork.com)


    helveticus wrote:
    > I'm having difficulties setting the expires header for images and css
    > files in my compression HttpModule. Compression is carried out by
    > context_BeginRequest and works fine (code not shown). However, I
    > can't set the expiration header for selected file extensions (ie. gif
    > and css). Firebug' s YSL reports that these components do not have a
    > far future Expires header.
    >
    > Question: How should I modify the code to set the expires header? TIA
    > for any hints.
    >
    > Public Class CompressionModule
    > Implements IHttpModule
    >
    > Public Sub Dispose() Implements System.Web.IHttpModule.Dispose
    > End Sub
    >
    > Public Sub Init(ByVal context As System.Web.HttpApplication)
    > Implements System.Web.IHttpModule.Init
    > AddHandler context.BeginRequest, AddressOf
    > context_BeginRequest ' Carries out compression
    > AddHandler context.EndRequest, AddressOf
    > context_EndRequest
    > End Sub
    >
    > Public Sub context_EndRequest(ByVal sender As Object, ByVal e
    > As EventArgs)
    > Dim app As HttpApplication = CType(sender,
    > HttpApplication)
    >
    > If app.Response.ContentType = "image/gif" Or
    > app.Response.ContentType = "text/css" Then
    > app.Response.ExpiresAbsolute =
    > DateTime.Now.AddMinutes(5)
    >
    > app.Response.Cache.SetCacheability(HttpCacheability.Public)
    > End If
    > End Sub
    > End Class
     
    bruce barker, Sep 14, 2008
    #2
    1. Advertising

  3. helveticus

    helveticus Guest

    Thanks for your reply. I'll check with my ISP to have the headers set
    via IIS.
     
    helveticus, Sep 15, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VP
    Replies:
    3
    Views:
    529
    Joerg Jooss
    Sep 14, 2005
  2. Replies:
    1
    Views:
    579
    clintonG
    Dec 30, 2005
  3. Tine Müller
    Replies:
    1
    Views:
    957
    Johannes Koch
    Jul 28, 2007
  4. helveticus
    Replies:
    0
    Views:
    323
    helveticus
    Sep 14, 2008
  5. Mark Volkmann

    setting Expires HTTP response header

    Mark Volkmann, Jul 16, 2005, in forum: Ruby
    Replies:
    2
    Views:
    375
    David Heinemeier Hansson
    Jul 17, 2005
Loading...

Share This Page