在ASP.NET MVC中更新多值cookie?

3

如何在ASP.NET中更新多值cookie?

1个回答

12
public ActionResult ModifyCookie()
{
    // Read the cookie from the request
    var cookie = Request.Cookies["cookieName"];

    // Verify that the cookie was present
    if (cookie != null)
    {
        // modify a value given the key
        cookie.Values["key"] = "modified value";

        // write the modified cookie back to the response
        Response.AppendCookie(cookie);
    }
    return View();
}

Response.AppendCookie(...) 是我所缺少的。谢谢。 - Krisztián Balla
1
实际上,更新现有的 cookie 应该使用:Response.SetCookie(cookie)Response.AppendCookie 只是将 cookie 添加到集合中。 - Captain Betty

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接