17

how to detect when a page is called using ajax in asp.net mvc ?

6 Answers 6

35

According to the Professional ASP.NET MVC 1.0 book, the MVC AJAX library will insert a form field called "X-Requested-With" with a value of "XMLHttpRequest".

You can then use an extension method in System.Web.Mvc that means you can simply call Request.IsAjaxRequest() and get a simple true or false saying if this is an AJAX request.

Sign up to request clarification or add additional context in comments.

2 Comments

While this will be reliable in most cases, I think the asker should be aware that this can easily be spoofed.
Please note: On a project with a large number of request, I randomly got Ajax requests from mobile browsers that had Request.IsAjaxRequest() returning false!!! I had to add a querystring parameter (like partial=1) to my requests to be on the safe side.
8

You can check it manually like this:

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

Or when you're in a Controller in ASP.NET MVC, which references System.Web.Mvc you will get an extension-method on the HttpRequestBase object, which you can access within an ActionMethod like this:

bool isAjaxRequest = Request.IsAjaxRequest();

1 Comment

Please note: On a project with a large number of request, I randomly got Ajax requests from mobile browsers that had Request.IsAjaxRequest() returning false!!! I had to add a querystring parameter (like partial=1) to my requests to be on the safe side.
5

There is no specific way to determine if the call was made by javascript or directly in the browser, as it is a regular http call.

You can add a header to your ajax call to distinguish it from other calls, or possibly add a parameter to the query string that is only used on ajax calls.

ASP.NET MVC ajax does add such a header - X-Requested-With: XMLHttpRequest, which you can use to sniff that this is an ajax call by the mvc ajax library. However, if you are using jQuery or your own hand rolled ajax calls, this will not be set. Additionally, other clients might spoof this header (using WebClient, for example) so finding it is not a guarantee that an ajax call has been made.

1 Comment

+1 for suggesting a parameter as on a project with a large number of requests, I randomly got Ajax requests from mobile browsers that had Request.IsAjaxRequest() returning false!!! I had to add a querystring parameter (like partial=1) to my requests to be on the safe side.
4

The best way to check if the request is an ajax request is to check Request.IsAjaxRequest(). It's good to know that under the hood, MVC framework checks for ajax requests in the Request Parameters OR the Request Header. The code in ASP.Net MVC source code is:

    public static bool IsAjaxRequest(this HttpRequestBase request) {
        if (request == null) {
            throw new ArgumentNullException("request");
        }

        return (request["X-Requested-With"] == "XMLHttpRequest") || ((request.Headers != null) && (request.Headers["X-Requested-With"] == "XMLHttpRequest"));
    }

So if you want to check it mannually (which is not recommended) you have to check both.

Comments

0

You would need to pass some parameter with your AJAX call - AJAX is just a GET request, no different then typing a url into the address bar and pressing enter (this is why AJAX must be guarded against cross site scripting attacks, otherwise an attacker can force people to execute AJAX commands to your site just by including the url in an image)

Comments

-7

Why does it matter? It shouldn't. Are you really trying to do content negotiation?

10 Comments

It DO matter. For instance, you might not want to return a complete HTML page, but only a partial.
Then add a parameter to the url. Serving up different content just because the request originated from a different type of client is bad design. This method locks that behavior in for ajax requests. What if you later want to get the entire page content via ajax (Oops, can't). What if you later write a desktop app that wants partial content (Oops, can't do that either, since it's not an ajax request).
Are you serious? Why would you ever want to fetch a complete html page over ajax? There is nothing to gain compared to using a normal request. Write a desktop client that want's partial content? are you building yourself a web browser? ;) Serving up different content just because the request originated from a different type of client is bad design The client is the same, it's only how the request are fetched that differ.
Perhaps the page isn't the page currently being displayed (jQuery clueTip plug-in, for example). Perhaps I'm building the next twitter, and I want to support multiple clients, not just browsers. I want native iPhone and Andriod apps that aren't forced to get the entire page in a single shot. And I certainly don't want to re-code the site every time I discover another platform I want to be on.
And the XMLHttpRequest is a separate client, with separate capabilities. Will it parse the html and render it? Will it request css, js and image files in an html response? Not the same as the browser itself.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.