gpt4 book ai didi

c# - 从 cookie 创建和读取列表<>

转载 作者:太空狗 更新时间:2023-10-29 23:53:24 29 4
gpt4 key购买 nike

我正在尝试显示最近查看的产品,到目前为止我已经这样做了。我有一个 Product 表,其中存储了许多产品。我有 HomeController,它有一个显示产品详细信息的 Details() 操作方法。

我编写了 AddRecentProduct 方法,它在 Session 中存储最近查看的产品 (10)

现在我想将这些最近查看的产品列表存储到访问者计算机上至少 30 天的 cookie 中,因为 session 已过期。就像最近查看的 Imdb。

此外,如果我在数据库 RecentlyViewed 中创建另一个表,其中包含 rv_id、userId、productId 列,我将如何在其中保存 recentlyViewedList 数据? userId 列将保存已登录用户的 ID,但如果用户是访客(未注册)怎么办,那么解决方案是什么?那我需要使用 GUID 吗?

RecentProduct.cs

public class RecentProduct
{
public int ProductId { get; set; }

public string ProdutName { get; set; }

public string ImageUrl { get; set; }

public DateTime LastVisited { get; set; }
}

Controller

    public void AddRecentProduct(List<RecentProduct> list, int id, string name, int maxItems)
{
var item = recentProductList.FirstOrDefault(t => t.ProductId == id);
if (item == null)
{
list.Add(new RecentProduct
{
ProductId = id,
ProdutName = name,
LastVisited = DateTime.Now,
});
}
while (list.Count > maxItems)
{
list.RemoveAt(0);
}
}

public ActionResult Details(int? id)
{
if (id == null)
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
Product product = db.Products.Find(id);
if (product == null)
return HttpNotFound();

var list = Session["RecentProductList"] as List<RecentProduct>;
if (list == null)
{
list = new List<RecentProduct>();
Session["RecentProductList"] = list;
}
AddRecentProduct(list, id.Value, product.Name, 10);
ViewData["RecentProductList"] = list;
return View(product);
}

产品详情查看页面

<div class="col-sm-9">
@{
var recentProductList = ViewData["RecentProductList"] as List<Project.Models.RecentProduct>;
}

@foreach (var recentProduct in recentProductList)
{
<p>@recentProduct.ProdutName (id: @recentProduct.ProductId) </p>
}
</div>

我通过 session 得到了想要的结果,现在我想用 cookie 做同样的事情。

这就是我正在尝试的 创建 cookie:

List<RecentProduct> yourList = new List<RecentProduct>();
RecentProduct rc = new RecentProduct();
rc.ProdutName = product.Name;
rc.ProductId = product.ProductId;
rc.ImageUrl = product.ImagePath;
rc.LastVisited = DateTime.Now;
yourList.Add(rc);

var yourListString = String.Join(",", yourList);
// Create a cookie
HttpCookie yourListCookie = new HttpCookie("YourList", yourListString);

// The cookie will exist for 7 days
yourListCookie.Expires = DateTime.Now.AddDays(7);

// Write the Cookie to your Response
Response.Cookies.Add(yourListCookie);

并且在ProductDetails View Page中读取这样的cookies:

 @if (Request.Cookies["YourList"] != null)
{
// Your cookie exists - grab your value and create your List
List<string> yourList = Request.Cookies["YourList"].Value.Split(',').Select(x => Convert.ToString(x)).ToList();

// Use your list here
<p>@yourList</p>

}

我没有得到任何结果。我如何读取 cookie 和值?

最佳答案

最适合您的解决方案是使用 Html5 网络存储。
它可以让您在本地浏览器中存储最多 5mb。
你只能通过javascript读写。
示例:

    $('#btnLocalStorage').click(function()  
{
var txtName = $("#txtName").val();
//set item
localStorage.setItem("EmpName", txtName);
});

$('#btnLocalStorageRemove').click(function()
{
//remove item
localStorage.removeItem("EmpName");
});
$('#btnLocalStorageClear').click(function()
{
//clear Local Storage
localStorage.clear();
});
$('#btnLocalStorageLength').click(function()
{
var localStoragelength = localStorage.length;
alert("The length of Local storage is " + localStoragelength);

});

顺便说一下,它没有过期时间,你不需要 guid。

关于c# - 从 cookie 创建和读取列表<>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34142130/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com