我编写了一个图像处理程序,该处理程序还通过在满足某些条件时发送304来实现客户端缓存。 为了访问不同的照片或登录的人自己的照片,我们以以下方式将处理程序放置在不同的页面上。
问题是,如果我在图像被缓存在页面上后第二次单击链接来访问任何页面,则它不会命中处理程序(调试器不会命中)。 但是,如果我使用F5或“刷新”按钮刷新该页面,则只有它会点击处理程序并显示我要显示的内容。
只有当我尝试使用附加的另一个参数(例如“&date =” + CurrentDate)来访问它时,它才会到达处理程序,但会杀死缓存的目的。
//or something like that
我知道代码没有错,因为它在被击中时可以很好地工作。 无论如何,这是代码。
public void ProcessRequest(HttpContext context)
{
var isDefault = true;
var textIfModifiedSince = context.Request.Headers["If-Modified-Since"];
context.Response.ClearHeaders();
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.Cache.SetExpires(DateTime.Now.AddMonths(6));
try
{
var isOwn = false;
var user = /*Gets logged in user instance from service*/;
var employeeId = 0;
if (context.Request.QueryString.Count > 0)
{
if(context.Request.QueryString[0] == "self")
{
if (user != null)
{
employeeId = Convert.ToInt32(user.EmployeeId);
isOwn = true;
}
}
else if (!int.TryParse(context.Request.QueryString[0], out employeeId))
employeeId = 0;
}
else
{
if (user != null)
{
employeeId = Convert.ToInt32(user.EmployeeId);
isOwn = true;
}
}
if (user != null && employeeId != 0)
{
var employee = GetEmployee(employeeId);
if (
(!string.IsNullOrEmpty(textIfModifiedSince) && employee.Modify_Date == null)
|| !string.IsNullOrEmpty(textIfModifiedSince)
&& employee.Modify_Date != null
&& employee.Modify_Date <= Convert.ToDateTime(textIfModifiedSince).AddMinutes(1))
{
isDefault = false;
context.Response.Cache.SetLastModified(Convert.ToDateTime(textIfModifiedSince));
context.Response.Status = "304 Not Modified";
HttpContext.Current.ApplicationInstance.CompleteRequest();
return;
}
if (
employee != null
&& !string.IsNullOrEmpty(employee.Picture)
&& (isOwn || employee.LocationID != null)
&& (isOwn || HasRequiredRoles))
{
var path = context.Server.MapPath("~//" + EmployeePhotoPath);
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
var fileName = employee.Picture;
var destinationPath =
context.Server.MapPath("~//" + EmployeePhotoPath).ToString(CultureInfo.InvariantCulture);
if (File.Exists(destinationPath + fileName))
{
isDefault = false;
context.Response.Cache.SetLastModified(employee.Modify_Date > DateTime.Now ? DateTime.Now : (employee.Modify_Date ?? DateTime.Now));
context.Response.ContentType = GetContentType(employee.Picture);
context.Response.WriteFile(EmployeePhotoPath + employee.Picture);
HttpContext.Current.ApplicationInstance.CompleteRequest();
return;
}
isDefault = false;
DownloadFromSFTP(path, employee.Picture, user, employeeId.ToString(CultureInfo.InvariantCulture));
context.Response.Cache.SetLastModified(employee.Modify_Date > DateTime.Now ? DateTime.Now : (employee.Modify_Date ?? DateTime.Now));
context.Response.ContentType = GetContentType(employee.Picture);
context.Response.WriteFile(EmployeePhotoPath + employee.Picture);
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
}
}
catch (Exception ex)
{
Log.Debug("Photo Handler Failed.", ex);
}
finally
{
if (isDefault)
{
if (!string.IsNullOrEmpty(textIfModifiedSince))
{
context.Response.Cache.SetLastModified(Convert.ToDateTime(textIfModifiedSince));
context.Response.Status = "304 Not Modified";
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
context.Response.Cache.SetLastModified(DateTime.Now);
context.Response.ContentType = GetContentType("images/avatar.gif");
context.Response.WriteFile("images/avatar.gif");
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
}
}
另外:在以下情况下会出现问题。
用户通过访问EmployeePhoto.ashx?Eid = 20349访问图像
管理员将照片更改为Eid = 20349
用户再次访问该页面(不刷新,而是从某个链接访问该页面)
页面上的缓存图像带来了旧图像,如果它已命中了处理程序,则说明情况已经在那里处理了,但是没有,页面显示了缓存版本本身。 我只在Chrome上检查过它。