设置ASP。网络缓存控制头

时间:2022-10-03 03:51:19

In my asp.net forms app. I'm trying to prevent caching of certain pages. to do so I've set a series of cache control headers using Response.AppendHeader e.g.

在我的asp.net表单应用程序中,我试图阻止某些页面的缓存。为此,我使用Response设置了一系列缓存控制头。AppendHeader如。

protected override void OnLoad(Eventargs e) 
{
...
Response.ClearHeaders();
Response.AppendHeader("Cache-Control","no-cache");
....
base.OnLoad(e);
}

Trouble is when I view my site and look at the Net tab in the Firefox console to view the request/response headers I see a behavior as follows:

问题是,当我查看我的站点并查看Firefox控制台的Net选项卡以查看请求/响应头时,我看到的行为如下:

  1. a POST request/response for the page1.aspx which triggers a redirect (to page2.aspx) the response here contains the correct headers.

    page1的POST请求/响应。触发重定向的aspx(到page2.aspx)这里的响应包含正确的标题。

  2. a GET request/response for page2.aspx associated response has the cache-control header just has a value 'pre-check=0'

    page2的GET请求/响应。aspx相关响应缓存控制头只有一个值' precheck =0'

That second request seems to allow the caching to happen for the page.. note: page1.aspx and page2.aspx both have the OnLoad logic I describe above. Also if I take some action on page2.aspx, the POST response will again have the correct headers.

第二个请求似乎允许对页面进行缓存。注意:所述。aspx和所以page2。aspx都具有上面描述的OnLoad逻辑。如果我在page2上采取一些行动。aspx, POST响应将再次有正确的标题。

What am I missing here? My expectation was that with the logic in OnLoad should mean that I always get the headers in the response and therefore 'always' get the current version of the page?

我错过了什么?我的期望是,使用OnLoad中的逻辑应该意味着我总是在响应中获得头信息,因此总是得到页面的当前版本?

What I'm seeing is firefox loading it's cached version of the page.

我看到的是firefox正在加载页面的缓存版本。

I'm considering creating a random identifier in the request url to force the matter, but that seems a little heavy handed.

我正在考虑在请求url中创建一个随机标识符来强制执行,但这似乎有点费力。

-----update----- It seems that, this may be related to having the caching code in 'OnLoad'. I've added these headers to the Page_Load() of the page and it works fine? any thoughts.

----更新----看起来,这可能与在“OnLoad”中拥有缓存代码有关。我已经将这些页眉添加到页面的Page_Load()中,它可以正常工作吗?任何的想法。

2 个解决方案

#1


2  

Try swapping out Response.AppendHeader("Cache-Control","no-cache"); for

尝试交换Response.AppendHeader(“cache - control”、“no - cache”);为

Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();

and make sure that you do not have an @ OutputCache directive and are not explicitly preventing cache control. See this MSDN page on SetCacheability and SetNoStore.

并确保您没有@ OutputCache指令,并且没有显式地阻止缓存控制。请参阅关于setcachability和SetNoStore的MSDN页面。

#2


0  

You might try this page-directive instead:

你可以试试这个页面指令:

<%@ OutputCache Location="None" VaryByParam="None" %>

See also: How to: Set the Cacheability of an ASP.NET Page Declaratively

参见:How to:设置ASP的可缓存性。网络页面声明

#1


2  

Try swapping out Response.AppendHeader("Cache-Control","no-cache"); for

尝试交换Response.AppendHeader(“cache - control”、“no - cache”);为

Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();

and make sure that you do not have an @ OutputCache directive and are not explicitly preventing cache control. See this MSDN page on SetCacheability and SetNoStore.

并确保您没有@ OutputCache指令,并且没有显式地阻止缓存控制。请参阅关于setcachability和SetNoStore的MSDN页面。

#2


0  

You might try this page-directive instead:

你可以试试这个页面指令:

<%@ OutputCache Location="None" VaryByParam="None" %>

See also: How to: Set the Cacheability of an ASP.NET Page Declaratively

参见:How to:设置ASP的可缓存性。网络页面声明