8

Update

I'm implementing a custom page caching solution and I don't want the request to be cached or retrieved from the cache if it's in response to a form submission or some sort of asp.net postback.

I'm trying to figure out if the current HttpRequest is a postback. Is there a way of doing this outside the context of a page or other usercontrol? In otherwords if I'm inside an HttpModule I don't have access to this.IsPostBack but I still need to determine if it is in fact a postback.

Also, are postbacks always "Post" requests or is that determined by containing form?

thanks!

Micah
  • 101,237
  • 81
  • 221
  • 320

6 Answers6

10

Check the Method property of the HttpWebRequest. Postbacks should be marked as POST in the Method.

Also, the way you did it in old-school asp was to check for expected post-back parameters in the body of the HTTP message (Request.Form). You could check the content of the request for data that looks like postback parameter. I'm not sure what object exactly you're working with, but if it's an HttpWebRequest, you might check the request stream from the GetResponseStream() method of the object.

Ben McCormack
  • 29,788
  • 45
  • 138
  • 211
6
if (Request.ServerVariables["REQUEST_METHOD"] == "POST") {
    // This is a POST
}
jessegavin
  • 67,411
  • 26
  • 135
  • 162
5

The following static routine should be able to determine whether or not the current request is a postback. However, it will only work if you are executing an ASPX page, or derivitive thereof.

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

Should also note that CurrentHandler may not yet be initialised depending upon which stage in the request lifecycle you attempt to evaluate IsPagePostBack. I believe this method will only be valid between HttpApplication.PostMapRequestHandler and HttpApplication.ReleaseRequestState.

Rabid
  • 2,874
  • 2
  • 22
  • 25
4

An HTTPHandler will have access to the current HTTPContext. You use that to check a few properties (Request.RequestType, Request.URLReferrer) and manually decide if it's a PostBack.

ThatSteveGuy
  • 1,075
  • 7
  • 11
1

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

Then you can check for the Page.IsPostBack property.

Community
  • 1
  • 1
David
  • 5,046
  • 2
  • 24
  • 37
  • This is good find and I'm up-voting it but it still wont work for me because I'm tapping into the pipeline before the handler gets associated with the request and therefore HttpContext.Current.Handler is always null. – Micah Aug 05 '10 at 17:30
  • During which step of page rendering do you need to know whether the page is postback or not? – David Aug 05 '10 at 17:39
0

Like @ThatSteveGuy's answer HTTPHandler will have access to the current page and you can check for the request type.

Note HTTPHandler context will be avaliable only in Application_PreRequestHandlerExecute in Global.asax.cs. Below is the code sample you can use it in your Global.asax.cs.

    void Application_PreRequestHandlerExecute(object sender, EventArgs e)
    {
        if (HttpContext.Current.Request.RequestType.Equals("POST"))
        {
            //Your code can go here.
        }
    }
Varun Nair
  • 144
  • 11