简体   繁体   中英

How to stop an ASP.NET webforms page caching different versions

I have a default page that can display 3 different contents depending on whether:

  1. The user is not logged in
  2. The user is logged in but hasn't set something up yet
  3. The user is logged in and has set something up

What's happening is that (1) and (2) will sometimes be displayed when it should be the other one. After I ctrl-r, the correct version displays.
This is nothing to do with the browser back button, it happens after clicking a menu option to go to the default page or performing some action that takes the user to the default page.

I also have a route set up for the page such that it's possible to append it with a username. eg: http://www.example.com/user1234

I mention this in case it may have something to do with it.

This is what I've tried to stop the caching:

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

That didn't work so I tried (in Page_Load):

Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache)
Response.Cache.SetNoStore()

Any ideas what I'm doing wrong?

ETA

As per a couple of comments, I tried it in Chrome incognito mode and there is no problem. I tried with the console open as well to see the headers but unfortunately the problem goes away then. Here's the headers anyway:

Request

GET / HTTP/1.1 Host: localhost:2873 Connection: keep-alive
Cache-Control: max-age=0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp, / ;q=0.8 User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.92 Safari/537.36 Referer: .../signin Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8
Cookie: ...

Response

HTTP/1.1 200 OK Cache-Control: no-cache, no-store Pragma: no-cache Content-Type: text/html; charset=utf-8 Content-Encoding: gzip
Expires: -1 Vary: Accept-Encoding Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319 X-SourceFiles: =?UTF-8?B?...
X-Powered-By: ASP.NET Date: Thu, 27 Mar 2014 20:17:39 GMT
Content-Length: 3045

ETA2

I've been testing in Chrome and I've just found out there's no problem in FireFox.

If I login/out/in/out... there is no problem
If I login/out then log in with the wrong password a couple of times (causing postbacks in the login page), and then log in again correctly, it always displays the incorrect logout page in Chrome.

I was having the same problem. What I did was place this

Response.CacheControl = "No-cache";

within the page load before any of the other code runs. The issue that this solved for me was similar tor yours. When a user logs in, a drop down box is loaded with different stores that the user belongs too. If I changed their stores, they could navigate back to the page and see their old set of stores. I placed the above in the page load and this solved my issues within IE, Chrome and Firefox. Hopefully something as easy as this will fix your problem.

To resolve the caching issue with particular file you have to add

<location path="WebForm1.aspx">
        <system.webServer>
            <caching enabled="false" enableKernelCache="false" />
        </system.webServer>
</location>

into your web.config's configuration section.

You can also do it using IIS. Steps to setup no cache using IIS is as below.

  1. Go to your sites Content View. Right click on file and switch to features view.

在此输入图像描述

  1. With file selected open Output Caching open from IIS.

在此输入图像描述

  1. From Action Pane on right side click on Edit Feature Setting, and from dialog box untick Enable cache and Enable kernal cache . Click Ok to save setting into web.config.

在此输入图像描述

看看这里并检查它是否有希望http://dotnet.dzone.com/articles/programmatically-clearing-0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM