Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect ajax call, ASP.net

how can I detect if a request is an AJAX-request (from jQuery) on the server-side of a ASP.net application. I don´t want to do this: mypage.aspx?this_is_ajax=true...

ex.

$.get("mypage.aspx");

On the server side I want to do certain things when the request is a ajax request...

Thanks!

like image 915
dale Avatar asked Aug 09 '10 15:08

dale


1 Answers

ASP.NET MVC had a great extension for this, that did work with JQuery. It checked it this way:

Check the core collection:

request["X-Requested-With"] == "XMLHttpRequest"

Check the headers collection (ensuring its not null):

request.Headers["X-Requested-With"] == "XMLHttpRequest"

It did it as an extension method, so you could recreate it, or use it if you are using ASP.NET MVC through Request.IsAjaxRequest().

like image 159
Brian Mains Avatar answered Nov 06 '22 12:11

Brian Mains