About
Profile picture Gokhan Demir,
a computer geek in Istanbul.

E-mail me Send mail


Photo Of The Day


  • Island at Twilight, South Africa, 2003

Calendar

<<  August 2008  >>
MoTuWeThFrSaSu
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

View posts in large calendar

Pages


    Recent posts


    Recent comments


    Archive


    Authors


    Tags


    Categories


    Blogroll


    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2008

    Sign in

    Detecting Asp.net Ajax Request In Custom HttpModule

    by Gokhan Demir 10/29/2007 6:50:00 PM

    The UpdatePanel is one of the coolest feature of Microsoft's ASP.NET AJAX. For some reason if you want to detect an ajax request on the page code-behind it's so easy. Take a look at the code snipped below :

    if(ScriptManager.GetCurrent(this).IsInAsyncPostBack)
    {
    	//this is an ajax postback
    }
    

    On the other hand if you use a custom httpmodule with asp.net ajax web application, you will probably get this error  :

    Sys.WebForms.PageRequestManagerParserErrorExeption: The message received from the server could not be parsed.  Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.  Details: ...

    In this situation, we can not use ScriptManager's IsInAsyncPostBack property cause we're working at the httpmodule level. We need something else to catch ajax request.So I began to look for a special property which represents the ajax request. Look at the page's request headers :


    Cache-Control : no-cache
    Connection : Keep-Alive
    Content-Length : 720
    Content-Type : application/x-www-form-urlencoded
    Accept : */*
    Accept-Encoding : gzip, deflate
    Accept-Language : tr
    Cookie : ASP.NET_SessionId=gytqakjnhobwd0utbwd5fo55
    Host : localhost:3548
    Referer : http://localhost:3548/WebForm1.aspx
    User-Agent : Mozilla/4.0 (compatible; MSIE 7.0
    x-microsoftajax : Delta=true
    UA-CPU : x86

    Whoala ! Asp.Net Ajax adds an extra header tag for it's request. Just look for the 'x-microsoftajax' tag on HttpModule's BeginRequest event. If it is existing then it's an ajax request. 

    if (Request.Headers["x-microsoftajax"] != null)
    {
    	//ajax request
    }
    
    Powered by BlogEngine.NET
    Designed by FYFI, Adapted by Gokhan Demir