Monday, August 8, 2011

How To Tell If a Request Is An AJAX Request (Microsoft MVC)

There might be times when you want to handle an AJAX http request differently than a "normal" http request.

It's really quite simple - just check the request header!

if (Request.Headers["X-Requested-With"] != null
    && Request.Headers["X-Requested-With"] == "XMLHttpRequest"){
   // if here this is an AJAX Request
}


The point is, take a look at the Headers attribute of the Request and check using the code above.

Hope this post saves someone some time!

No comments:

Post a Comment