动态图像缓存handler.ashx不缓存

时间:2014-03-14 08:07:20

标签: asp.net vb.net iis

我接管了一个项目,该项目使用handler.ashx自动生成不同大小的图像版本(如果它们不存在)。此外,它应该提供现有图像并在客户端浏览器中缓存它们。但是对图像的所有请求都返回200而不是缓存。

当我使用fiddler查看请求时,图像的初始响应显示

InitialResponse 收到图像后的第二个请求 secondrequest secondresponse

<%@ WebHandler Language="VB" CodeBehind="GenericHandler.ashx.vb" Class=".GenericHandler" %>

Imports System.IO
Imports System.Web
Imports System.Web.Services
Imports System.Web.Script.Serialization
Imports System.Globalization

Public Class GenericHandler
    Implements System.Web.IHttpHandler, System.Web.SessionState.IRequiresSessionState

    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

        RequestHandler(context)

    End Sub
    Private Sub RequestHandler(ByVal context As HttpContext)
        Select Case context.Request.HttpMethod
            Case "GET"
                GetFile(context)
            Case Else
                context.Response.ClearHeaders()
                context.Response.StatusCode = 405
        End Select
    End Sub

    Private Sub GetFile(ByVal context As HttpContext)
        Dim strAssetId As String = context.Request("asset")
        Dim strSize As String = context.Request("size")
        Dim fileName As String = getAssetName(strAssetId)
        Dim internalFile As String = CalculateFileName(context, fileName)
        Dim refresh As TimeSpan = New TimeSpan(1, 0, 0, 0) '1 day cache
        Dim encode As Boolean = context.Request("encode") = "1"

        'we have already created the file
        If File.Exists(internalFile) Then

            'has the browser already recieved a cached version
            If Not String.IsNullOrEmpty(context.Request.Headers("If-Modified-Since")) Then
                Dim provider As CultureInfo = CultureInfo.InvariantCulture
                Dim lastMod As DateTime = DateTime.ParseExact(context.Request.Headers("If-Modified-Since"), "r", provider).ToLocalTime()

                'cached but is it within the expiry range                
                If lastMod < DateTime.Now.Add(refresh) Then
                    context.Response.StatusCode = 304
                    Return
                End If
            End If
        Else
            'this size of image didn't exist so create it.
            GenerateFile(context, internalFile)

        End If
        context.Response.ClearContent()
        context.Response.AddHeader("Content-Disposition", "attachment; filename=""" + fileName + """")
        context.Response.Cache.SetExpires(DateTime.Now.Add(refresh))
        context.Response.Cache.SetMaxAge(refresh)
        context.Response.Cache.SetCacheability(HttpCacheability.Public)
        context.Response.Cache.SetValidUntilExpires(True)
        context.Response.ContentType = "image/png"

        If encode Then
            context.Response.Write(ImageBase64Encoder.Encode(internalFile))
        Else
            context.Response.WriteFile(internalFile)
        End If
    End Sub

    Private Function CalculateFileName(ByVal context As HttpContext, fileName)
        'calculates the filename of the requestedfilesize
    End Function

    Private Sub GenerateFile(ByVal context As HttpContext, internalFile As String)
        'generates the correct sized version of the original image
    End Sub


    ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class

1 个答案:

答案 0 :(得分:1)

第二次请求的请求标题不包含您​​在代码中检查的If-Modified-Since标题。

  

为什么不呢?

因为第一个响应不包含Last-Modified标题。

  

如何设置Last-Modified标题?

您可以使用:context.Response.Cache.SetLastModified(myfile.LastWriteTime);