1

我想计算用户观看产品的次数。

产品具有整数类型的ViewCount属性,在 Details 视图中,我增加ViewCount

public ActionResult Details( int id )
  {
    ...
    product.ViewCount = product.ViewCount + 1;
    db.SaveChanges();
    return View( product );
  }

但在每次刷新时,ViewCount 都会增加。

我应该怎么做 ViewCount 在每个用户会话中增加 1 倍?

或者其他方式,正确的标签,任何链接,请。

4

5 回答 5

6

你可以使用cookie。

var productId = 1;


if (Request.Cookies["ViewedPage"] != null)
{
    if (Request.Cookies["ViewedPage"][string.Format("pId_{0}",productId )] == null)
    {
        HttpCookie cookie = (HttpCookie)Request.Cookies["ViewedPage"];
        cookie[string.Format("pId_{0}",productId )] = "1";
        cookie.Expires = DateTime.Now.AddDays(1);
        Response.Cookies.Add(cookie); 

        db.Execute("UPDATE Products SET ViewCount = ViewCount + 1 WHERE ProductId = @id", new { id = productId } ); 
        db.SaveChanges();
    }
}
else
{
    HttpCookie cookie = new HttpCookie("ViewedPage");
    cookie[string.Format("pId_{0}",productId )] = "1";
    cookie.Expires = DateTime.Now.AddDays(1);
    Response.Cookies.Add(cookie); 

    db.Execute("UPDATE Products SET ViewCount = ViewCount + 1 WHERE ProductId = @id", new { id = productId } ); 
    db.SaveChanges();
}
于 2012-08-31T15:15:51.873 回答
3

我建议您创建一些操作过滤器来跟踪它。

public class TrackFilter: IResultFilter
{
  public void OnResultExecuting(ResultExecutingContext filterContext)
  {

  }

  public void OnResultExecuted(ResultExecutedContext filterContext)
  {
    // do a database call and update the count
  }
}

[Track]
public ActionResult Details(int id)
{
}

您也可以参考此线程以获得更多想法。

于 2012-08-31T15:10:51.350 回答
2

您的代码不是线程安全的:如果两个用户同时访问该页面,则 ViewCount 将增加 1 而不是 2(因为该值由 DB 使用者而不是 DB 本身增加)。您可以通过直接运行 SQL 来解决此问题UPDATE Products SET ViewCount = ViewCount + 1 WHERE ProductId = @id。您的 DBMS 将提供并发保护。

无论如何,为了防止视图计数因同一会话中的其他视图而增加,只需使用存储在会话中的哈希集,如下所示:

public ActionResult Details(Int32 id ) {

    HashSet<Int32> productsSeen = (HashSet<Int32>)Session["ProductsSeen"];
    if( productsSeen == null ) {
        Session["ProductsSeen"] = productsSeen = new HashSet<Int32>();
    }

    if( !productsSeen.Contains( id ) ) {

        db.Execute("UPDATE Products SET ViewCount = ViewCount + 1 WHERE ProductId = @id", new { id = id } ); // psuedocode. This isn't a real EF construct.
        db.SaveChanges();
    }

    return View( product );
}
于 2012-08-31T13:16:10.760 回答
0
model.ViewCount = model.ViewCount + 1;

_context.Entry(model);

_context.SaveChanges();

return View(model);
于 2015-03-25T19:34:18.993 回答
0

这是改进的 Dai 代码。您必须添加 productsSeen.Add(id); 在第二个 if 语句中

public ActionResult Details(Int32 id ) {

    HashSet<Int32> productsSeen = (HashSet<Int32>)Session["ProductsSeen"];
    if( productsSeen == null ) {
        Session["ProductsSeen"] = productsSeen = new HashSet<Int32>();
    }

    if( !productsSeen.Contains( id ) ) {
        productsSeen.Add( id );
        db.Execute("UPDATE Products SET ViewCount = ViewCount + 1 WHERE ProductId = @id", new { id = id } ); // psuedocode. This isn't a real EF construct.
        db.SaveChanges();
    }

    return View( product );
}
于 2017-09-23T13:04:08.807 回答