C# 查看计数器未缓存

C# 查看计数器未缓存,c#,asp.net,C#,Asp.net,我在ASP.NET中将视图计数器创建为通用处理程序,这里是代码,方法AddPostView执行简单的SQL命令,如“view=view+1” 以下是我在页面标记中如何称呼它: <img id="counter" runat="server" src="" /> 但我无法将其缓存,也就是说,在用户首次访问此网站后,我尝试将其包装为css样式表,但同样的事情没有发生,当我在其上运行firebug时,它甚至没有在下载的图像或css下显示,但它仍然会点击,我得到了另一个计数的视图。我如何

我在ASP.NET中将视图计数器创建为通用处理程序,这里是代码,方法
AddPostView
执行简单的SQL命令,如“view=view+1”

以下是我在页面标记中如何称呼它:

 <img id="counter" runat="server" src="" />

但我无法将其缓存,也就是说,在用户首次访问此网站后,我尝试将其包装为css样式表,但同样的事情没有发生,当我在其上运行firebug时,它甚至没有在下载的图像或css下显示,但它仍然会点击,我得到了另一个计数的视图。我如何解决这个问题?

我想你在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);
}

我想你在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);
}

实际上,现在我正在获取图像,但响应是“200 OK”,而不是“304 Not modified”@user1010609未修改的是来自服务器的,如果使用特殊检查,200 OK是从本地缓存获取的。当我加载页面时,它仍然计数,我将尝试乱用缓存选项。@user1010609您不能100%确定这种类型的缓存。如果单击“重新加载”,浏览器可能会选择重新加载图像,即使图像已在缓存中。要解决此类型的问题,您可以使用会话或cookie,或将cookie与页面(用户与页面)连接。。。有时认为需要更多的代码和数据库来解决。好的,谢谢,我会尝试session。实际上现在我得到了图像,但响应是“200 Ok”,而不是“304 Not modified”@user1010609如果使用特殊检查,则未修改来自服务器,如果它是从本地缓存中获取的,那么200是可以的。当我加载页面时,它仍然会计数,我会尝试乱用缓存选项。@user1010609您不能100%确定这种类型的缓存。如果单击“重新加载”,浏览器可能会选择重新加载图像,即使图像已在缓存中。要解决此类型的问题,您可以使用会话或cookie,或将cookie与页面(用户与页面)连接。。。有时认为需要更多的代码和数据库来解决。好的,谢谢,我将尝试会话。
// 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);
}