我们正在使用.net framework 4.0在asp.net网站上进行工作。并且我们尝试为其合并输出缓存。
但是不幸的是它没有用。后来我们发现删除Microsoft安全更新KB2656351将解决此问题。
我想知道是否还有其他方法可以执行此操作而不删除更新。

最佳答案

仅当您安装上述更新并且响应中包含cookie时,才存在此问题。不管请求中是否包含cookie。找到了解决此问题的解决方法。我创建了一个自定义HTTPModule并将所有可用的cookie从响应(包括新添加的cookie)复制到 Context.Items 。然后清除响应中所有可用的cookie。

在下一步中,读取存储在Context.items中的对象,然后添加回响应中。因此,当输出缓存提供程序尝试缓存页面时,响应中没有cookie。因此它照常工作。然后再添加Cookie。

    public void Init(HttpApplication context)
    {
        context.PostReleaseRequestState += new EventHandler(OnPostReleaseRequestState);
        context.PostUpdateRequestCache += new EventHandler(OnPostUpdateRequestCache);
    }

    public void OnPostReleaseRequestState(Object source, EventArgs e)
    {
        HttpApplication application = (HttpApplication)source;
        HttpContext context = application.Context;
        HttpCookieCollection cookieCollection = new HttpCookieCollection();
        foreach (string item in context.Response.Cookies)
        {
            HttpCookie tempCookie = context.Response.Cookies[item];

            HttpCookie cookie = new HttpCookie(tempCookie.Name) { Value = tempCookie.Value, Expires = tempCookie.Expires, Domain = tempCookie.Domain, Path = tempCookie.Path };
            cookieCollection.Add(cookie);
        }
        context.Items["cookieCollection"] = cookieCollection;
        context.Response.Cookies.Clear();
    }

    public void OnPostUpdateRequestCache(Object source, EventArgs e)
    {
        HttpApplication application = (HttpApplication)source;
        HttpContext context = application.Context;
        HttpCookieCollection cookieCollection = (HttpCookieCollection)context.Items["cookieCollection"];
        if (cookieCollection != null)
        {
            foreach (string item in cookieCollection)
            {
                context.Response.Cookies.Add(cookieCollection[item]);
            }
        }
    }

关于asp.net - .net4.0输出缓存不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14211464/

10-12 00:03
查看更多