How to determine if a request is the result of a postback?

Update

I implement special page caching, and I do not want the request to be cached or fetched from the cache if it responds to form submission or some kind of rollback after asp.net.

I am trying to find out if the current HttpRequest is postback. Is there a way to do this outside the context of a page or other user control? In other words, if I'm inside an HttpModule, I don't have access to this.IsPostBack , but I still need to determine if this is a postback.

Also, are callbacks always β€œsent” requests or are defined using a form?

thanks!

+7
source share
5 answers

Check the Method property of HttpWebRequest . Parcels must be marked as POST in Method .

In addition, the way you did this in an old school asp was to check the expected feedback parameters in the body of the HTTP message ( Request.Form ). You can check the contents of the request for data that looks like a postback parameter. I'm not sure which object you are working with, but if it is HttpWebRequest , you can check the request flow from the GetResponseStream() method of the object.

+9
source share
 if (Request.ServerVariables["REQUEST_METHOD"] == "POST") { // This is a POST } 
+6
source share

The following static routine should be able to determine if the current request is a postback. However, it will only work if you are running an ASPX page or its derivatives.

 public class PostBackUtility { public static Boolean IsPagePostBack { get { Page pageHandler = HttpContext.Current.CurrentHandler as Page; if (pageHandler == null) return false; return pageHandler.IsPostBack; } } } 

It should also be noted that CurrentHandler may not yet be initialized depending on what stage of the request life cycle you are trying to evaluate IsPagePostBack . I believe that this method will only be valid between HttpApplication.PostMapRequestHandler and HttpApplication.ReleaseRequestState .

+5
source share

The HTTPHandler will gain access to the current HTTPContext. You use this to check several properties (Request.RequestType, Request.URLReferrer) and manually decide if this is a PostBack.

+3
source share

You can get a link to the current page: Get the current version of System.Web.UI.Page from HttpContext?

Then you can check the Page.IsPostBack property.

+1
source share

All Articles