我在ASP.NET中创建了视图计数器作为通用处理程序,这里是代码,方法AddPostView
执行简单的SQL命令,如“View = View +1”
public void ProcessRequest(HttpContext context)
{
if (UrlManipulation.IsRoutedValueInt(context.Request["ID"]))
{
int PostID = Convert.ToInt32(context.Request["ID"]);
PostManipulation.AddPostView(PostID);
}
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.Cache.SetExpires(DateTime.Now.AddMinutes(10));
context.Response.Cache.SetMaxAge(new TimeSpan(0, 10, 0));
}
public bool IsReusable
{
get
{
return true;
}
}
以下是我在页面标记中调用它的方式:
<img id="counter" runat="server" src="" />
我在页面加载时设置了src dinamicly:
counter.Src = "/handlers/CountView.ashx?ID=" + post.ID.ToString();
但我无法让它缓存,即。在用户第一次访问此网站后不计算额外的视图,我尝试将其包装为css样式表,但同样的事情没有任何反应,当我在它上面运行firebug时,它甚至没有在下载的图像或css下显示它但它仍然击中它我得到了另一个视图。我怎么解决这个问题呢?
答案 0 :(得分:1)
我认为你在img上设置它,但没有返回任何图像,所以浏览器没有缓存它。试试这段代码:
// 1x1 transparent GIF
private readonly byte[] GifData = {
0x47, 0x49, 0x46, 0x38, 0x39, 0x61,
0x01, 0x00, 0x01, 0x00, 0x80, 0xff,
0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
0x00, 0x2c, 0x00, 0x00, 0x00, 0x00,
0x01, 0x00, 0x01, 0x00, 0x00, 0x02,
0x02, 0x44, 0x01, 0x00, 0x3b
};
public void ProcessRequest (HttpContext context)
{
if (UrlManipulation.IsRoutedValueInt(context.Request["ID"]))
{
int PostID = Convert.ToInt32(context.Request["ID"]);
PostManipulation.AddPostView(PostID);
}
// set the cache
context.Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(10));
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.Cache.SetValidUntilExpires(true);
// send the image
context.Response.ContentType = "image/gif";
context.Response.Buffer = false;
context.Response.OutputStream.Write(GifData, 0, GifData.Length);
}