Monday, March 26, 2012

Detect AJAX postback in Application.EndRequest

Anyone know if there's a way to detect whether the current request is an AJAX postback or a regular postback from inside the Application.EndRequest event of an HttpModule? I have a module that writes out information to the end of a normal request (using response.write). However, this causes an AJAX postback request to fail. Or maybe there's a better way to append some text to the end of a request than response.write?

Ideas?

Test for (Request["X-MicrosoftAjax"] == "Delta=true"). That request header is present in partial postbacks.


Most excellent. Thanks!

HOWEVER, I'm a bit PEEVED at Microsoft for making the client-side AJAX javascript so sensitive to the data that comes back. If any part of my code does a response.write anywhere, the stupid AJAX Javascript on the client goes wobble-kneed and dies. Sheesh! I'd really like to be able to write out some text (using response.write) in both the master-page AND the httpModule's end_request event, but the AJAX Javascript on the client won't allow it! What a pain.

I've seen some c# code on the internet that shows how to modify the response that AJAX returns, but it's confusing as it's doing text translation (i.e., the function of the code is getting in the way of me understanding what it's doing to the response string).

Thanks for the answer to the original question.


Use some sort of HTTP viewer to inspect a partial postback response, and you'll probably see a way to do what you want to. Once you see it, hands-on, it's not too difficult to figure out how to alter (or see why Response.Write kills it).

I use FireBug for that, myself. I think Fiddler is a comparable IE addon.

No comments:

Post a Comment