gpt4 book ai didi

c# - ASP.NET MVC OutputCache 因 * 和用户 cookie 而异

转载 作者:IT王子 更新时间:2023-10-29 04:39:05 32 4
gpt4 key购买 nike

我有一个 asp.net mvc 3 项目和一个家庭 Controller 。我用这个属性标记了我的索引操作:

[OutputCache(Location = System.Web.UI.OutputCacheLocation.Any, Duration = 120, VaryByParam = "*", VaryByCustom = "user")]
public ActionResult Index()
{
return View();
}

在 Global.asax.cs 中处理用户自定义变化以检查用户 cookie 值,以便缓 stub 据用户是否登录以及他们是什么用户而变化。

当我在我的网络服务器上访问这个页面时,我在响应中得到这些 header :

Cache-Control   public, max-age=120
Content-Type text/html; charset=utf-8
Content-Encoding gzip
Expires Sun, 20 Mar 2011 21:50:09 GMT
Last-Modified Sun, 20 Mar 2011 21:48:09 GMT
Vary Accept-Encoding
Date Sun, 20 Mar 2011 21:48:09 GMT
Content-Length 3105

马上,Vary - Accept-Encoding 值看起来不对,它不应该发送一个 Vary - * 吗?

我也将 User.Identity.Name 属性呈现给这个 View ,我注意到即使我注销它仍然会呈现用户名,直到 120 秒到期。

public override string GetVaryByCustomString(HttpContext context, string custom)
{
if (custom.Equals("user", StringComparison.OrdinalIgnoreCase))
{
HttpCookie cookie = context.Request.Cookies["user"];
if (cookie != null)
{
return cookie.Value;
}
}
return base.GetVaryByCustomString(context, custom);
}

已经玩了几个小时了,完全卡住了,希望有人有一个想法......

最佳答案

您可以通过在 web.config 中的 urlCompression 元素上设置 dynamicCompressionBeforeCache="true" 让 IIS 在响应被缓存之前对其进行压缩。这将导致返回预期的 Vary:* header 。

摘自 IIS Configuration Reference , 关于 dynamicCompressionBeforeCache 属性:

The dynamicCompressionBeforeCache attribute specifies whether IIS will dynamically compress content that has not been cached. When the dynamicCompressionBeforeCache attribute is true, IIS dynamically compresses the response the first time a request is made and queues the content for compression. Subsequent requests are served dynamically until the compressed response has been added to the cache directory. Once the compressed response is added to the cache directory, the cached response is sent to clients for subsequent requests. When dynamicCompressionBeforeCache is false, IIS returns the uncompressed response until the compressed response has been added to the cache directory.

关于c# - ASP.NET MVC OutputCache 因 * 和用户 cookie 而异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5371773/

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