F5浏览器请求html资源超时,html - 在刷新页面之前(使用F5或“刷新按钮”),ASHX通用处理程序不起作用 - 堆栈内存溢出...

我编写了一个图像处理程序,该处理程序还通过在满足某些条件时发送304来实现客户端缓存。 为了访问不同的照片或登录的人自己的照片,我们以以下方式将处理程序放置在不同的页面上。

EmployeePhoto.ashx

EmployeePhoto.ashx?self

EmployeePhoto.ashx?default

问题是,如果我在图像被缓存在页面上后第二次单击链接来访问任何页面,则它不会命中处理程序(调试器不会命中)。 但是,如果我使用F5或“刷新”按钮刷新该页面,则只有它会点击处理程序并显示我要显示的内容。

只有当我尝试使用附加的另一个参数(例如“&date =” + CurrentDate)来访问它时,它才会到达处理程序,但会杀死缓存的目的。

14%2000:00:00 //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上检查过它。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值