返回的OutputCache无效的版本与回发 [英] OutputCache returns invalid version with PostBack
问题描述
我有一个奇怪的问题用的OutputCache发生。我有一个网页,其中之一是一个登录控制上的多个用户控件。页面,登录控制不缓存,但其他用户控件与VaryByParam时缓存。现在,这一切与高速缓存一起工作,当我点击不同的页面。但只要我登录,在该页面显示旧的缓存版本,其他用户控件。如果我刷新页面,我得到的所有用户控件的正确的缓存版本。问题是,只有当回发发生。有关回发某种原因,缓存版本返回没有考虑到的VaryByParam字符串。当这个网上搜索,我没有看到被要求在asp.net,其中有一个code解释这个类似的问题。
I am having a weird issue happening with outputcache. I have multiple user controls on a page, one of which is a login control. The page and the login control is NOT cached, but other user controls are cached with VaryByParam. Now all of this works along with caching when I click on to different pages. But as soon as I login, other user controls on that page display old cached versions. If I refresh the page, I get the correct cached version of all user controls. The problem is only when a postback happens. For some reason on a postback, the cached version returned does not take into account the VaryByParam string. When searching for this online, I did see a similar problem being asked on asp.net, which had a code explaining this.
为什么会回传导致缓存即可返回无效的版本?
Why would postback cause the cache to return back invalid version ?
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<%@ Register src="WebUserControl1.ascx" tagname="WebUserControl1" tagprefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<uc1:WebUserControl1 ID="WebUserControl11" runat="server" EnableViewState="false" />
</form>
</body>
</html>
WebUserControl1.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplication1.WebUserControl1" %>
<%@ OutputCache Duration="3600" VaryByParam="MenuID" %>
<asp:LinkButton ID="test" runat="server" Text="PostBack"></asp:LinkButton>
<br /><br />
<a href="Default.aspx?menuid=1">1</a> - <a href="Default.aspx?menuid=2">2</a> - <a href="Default.aspx?menuid=3">3</a>
<br /><br />
MenuID: <%= Request.QueryString["MenuID"] != null ? Request.QueryString["MenuID"].ToString() : "null" %>
运行演示,你会看到页面之间点击获取正确的缓存版本。但玩弄单击页面,造成回发,然后你会看到你得到错误的高速缓存版本的时候。
Run the demo and you will see clicking between pages gets the correct cached version. But play around with clicking a page and causing a postback and then you will see that you get the wrong cache version sometimes.
推荐答案
我觉得这是ASP.NET的一个bug,直到它解决了,这里是一个解决办法。
I think this is a bug in ASP .Net, and until its solved, here is a workaround.
对于每一个回传我希望有一个新的版本,而不是一个缓存版本。但除此之外我想缓存的版本。所以,我可以看看是否有什么样的要求,这是。如果这是一个后我会得到一个新的版本,如果这是一个'GET'我将获得从缓存中的版本。要做到这一点,我设置的用户控件VaryByCustom是一个缓存设置。而在我的Global.asax这样做:
For every postback I want a fresh version and not a cached version. But otherwise I want the cached version. So I can look if what kind of request this is. If this is a 'POST' I will get a new version, if this is a 'GET' I will get the version from cache. To do this, I setup a VaryByCustom cache setting on the usercontrol. And in my global.asax did this:
public override string GetVaryByCustomString(HttpContext context, string arg)
{
if (arg.Trim().ToLower() == "getorpost")
{
//for a POST request (postback) force to return back a non cached output
if (context.Request.RequestType.Equals("POST"))
{
return "post" + DateTime.Now.Ticks;
}
return "get";
}
return base.GetVaryByCustomString(context, arg);
}
这篇关于返回的OutputCache无效的版本与回发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!