I have written an image handler which also implements client side caching by sending 304 on meeting certain conditions. For accessing different photos or the logged in person's own photo, we put the handler in the following way on different pages.
<img src="EmployeePhoto.ashx" />
<img src="EmployeePhoto.ashx?self" />
<img src="EmployeePhoto.ashx?default" />
THE PROBLEM IS, if I access any page by clicking on a link the second time after the image has been cached on the page, it doesn't hit the handler (debugger wont hit). But if I refresh that page with F5 or refresh button, only then it hits the handler and show what I want to show.
Only when I try to access it with another parameter appended like "&date=" + CurrentDate, then it hits the handler but it kills the purpose of caching.
<img src="EmployeePhoto.ashx?default&date=03/31/14 00:00:00" /> //or something like that
I know there is nothing wrong with the code as it serves well when it is hit. Here is the code anyways.
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();
}
}
}
ADDITION: The problem occurs in the following scenario.
- A user accesses the image by accessing EmployeePhoto.ashx?Eid=20349
- Admin changes the photo for Eid=20349
- User accesses the page again (not refreshing but accessing the page from some link)
The cached image on the page brings the old image, if it had hit the handler then the situation has already been handled there, but it doesn't and the page shows the cached version itself. I checked it on chrome only.