gpt4 book ai didi

asp.net-mvc-3 - 使用 Reponse.Cookies 时 OutputCache 不工作

转载 作者:行者123 更新时间:2023-12-03 22:18:40 25 4
gpt4 key购买 nike

我正在尝试缓存 ActionResult。在特定的 ActionResult 中,我将一些数据写入 cookie。输出缓存在该操作结果中不起作用。它适用于我不使用 Response.Cookies 的所有其他操作。请帮我解决这个问题。

我正在使用 ASP.NET MVC 4

编辑

(包括代码)

 [OutputCache(Duration = 8000, VaryByParam = "*")]
public ActionResult List(SearchViewModel searchViewModel, int page = 1, int mode = 1)
{
HttpCookie ck = Request.Cookies["Geo"];
string lat = string.IsNullOrEmpty(Request.Params["lat"]) ? null : Request.Params["lat"];
string lng = string.IsNullOrEmpty(Request.Params["lng"]) ? null : Request.Params["lng"];
if (ck == null)
{
ck = new HttpCookie("Geo");
Response.Cookies.Add(ck);
}
if (lat != null)
{
ck["Lat"] = lat;
ck["Lon"] = lng;
ck.Expires = DateTime.Now.AddMonths(2);
Response.Cookies.Set(ck);
//this is the code which causes problem. If I remove this section catching will work
//other logic goes here..
}
}

最佳答案

请引用微软文档:
https://msdn.microsoft.com/en-us/library/system.web.httpcookie.shareable(v=vs.110).aspx

If a given HttpResponse contains one or more outbound cookies with Shareable is set to false (the default value), output caching will be suppressed for the response. This prevents cookies that contain potentially sensitive information from being cached in the response and sent to multiple clients.

To allow a response containing cookies to be cached, configure caching normally for the response, such as using the OutputCache directive or MVC's [OutputCache] attribute, and set all outbound cookies to have Shareable set to true.



所以基本上,请确保您设置了所有 cookie:
cookie.Shareable = true; // Needed with Outputcache

关于asp.net-mvc-3 - 使用 Reponse.Cookies 时 OutputCache 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17946559/

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