Reputation: 11861
I appear to have a problem and I cant find a solution....I have an ASP.NET search form and when user clicks on a result and then clicks the back button the browser, the user is then directed to a page that says 'Webpage has expired'
I know it s cache issue and this is what I have tried...
protected void Page_Init(object sender, EventArgs e)
{
Response.AppendHeader("Cache-Control", "no-cache");
}
and
protected void Page_Init(object sender, EventArgs e)
{
Response.AppendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
Response.AppendHeader("Pragma", "no-cache");
Response.AppendHeader("Expires", "0");
}
and none of these solution seem to work, any one have any idea why?
I also tried this...
Response.Cache.SetExpires(DateTime.Now.AddMinutes(-1));
but it didnt work.
I did a bit more digging and I used Fiddler to check my headers and it says Cache-Control: private, no-store, must-revalidate
I didn't set my Cache-Control to private anywhere...all I have is imports, is it possible one of my imports has a Cache-Control in them?
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CMS.UIControls;
using CMS.GlobalHelper;
using CMS.CMSHelper;
using CMS.Controls;
Upvotes: 1
Views: 6811
Reputation: 336
Already BenSwayne answered the question perfectly. In addition to that. This is common issue when you use ImageButton control of .Net E.g.
<asp:ImageButton ID="ibnDisplayImage" runat="server" ImageUrl="test.jpg"
PostBackUrl="~/home.aspx" />
So to avoide this issue. Use below code.
<a href="~/home.aspx"><img src="test.jpg" /> </a>
or
<asp:HyperLink ID="lnkDisplayImage" runat="server" NavigateUrl="~/home.aspx" > <asp:Image ID="imgDisplayImage" runat="server" ImageUrl="test.jpg" /> </asp:HyperLink>
Upvotes: 0
Reputation: 16900
This problem is not related to the cache control headers. You would see a similar error for search results pages with and without the cached headers.
The issue here is that your search form is being submitted via Http POST. This means the search field is being submitted as part of a POST body to the server. The browser views this like a form submission (think like a contact page submission). Browsers have built in mechanism to prevent re-posting the same form multiple times as inexperienced users may do this by accident without knowing it.
For example lets say you had a contact form that sent you a company email. A user might send you a message by POSTing the form, then click 'back', 'forward', 'back, 'forward', 'back', 'forward' and you wouldn't want to receive 3 or 4 copies of the same message. Therefor the browser warns you when you navigate back to a page that is the result of a form POST (some browsers will allow you click 'submit again' but this is not desirable).
Ok so what is the answer? You need to submit your search queries via an Http GET method. This normally results in the search string appearing in the query string of your search results page. For example Google encodes their search string in the querystring variable 'q' in most cases. You might have a page that looks like 'SearchResults.aspx?q=Test%20Phrase'. Notice the search phrase is provided in the querystring (as part of the page URL) and not in a POST body?
For more information on how to accomplish this within ASP.net's post centric way of doing things checkout this stackoverflow post. The example provided by @Solburn is the best example there as of this posting.
Upvotes: 6