net mvc:如何使用ajax检测何时调用页面

时间:2022-09-05 03:35:42

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

如何在asp.net mvc中检测何时使用ajax调用页面?

6 个解决方案

#1


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".

根据专业的ASP。MVC AJAX库将插入一个名为“X-Requested-With”的表单字段,值为“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.

然后可以在System.Web中使用扩展方法。这意味着您可以简单地调用request. isajaxrequest(),并得到一个简单的true或false,说明这是否是AJAX请求。

#2


7  

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:

或者当你在ASP的控制器中。它引用System.Web。Mvc,你会在HttpRequestBase对象上得到一个扩展方法,你可以在ActionMethod中访问它,比如:

bool isAjaxRequest = Request.IsAjaxRequest();

#3


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.

由于这是一个常规的http调用,因此没有特定的方法来确定调用是由javascript完成的还是直接在浏览器中完成的。

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.

您可以向ajax调用添加一个头,以将其与其他调用区分开来,也可以向仅用于ajax调用的查询字符串添加一个参数。

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.

ASP。NET MVC ajax确实添加了这样一个header - X-Requested-With: XMLHttpRequest,您可以使用它来嗅探这是MVC ajax库的ajax调用。但是,如果您正在使用jQuery或您自己的手工滚动的ajax调用,则不会设置这个参数。

#4


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:

检查请求是否是ajax请求的最好方法是检查request . isajaxrequest()。很高兴知道,在后台,MVC框架检查请求参数或请求头中的ajax请求。ASP中的代码。Net MVC源代码是:

    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.

所以如果你想要用人工检查(这是不推荐的),你必须同时检查这两个。

#5


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)

您需要传递一些参数与您的AJAX调用AJAX只是一个GET请求,然后在地址栏输入一个url没有什么不同和迫切的进入(这就是为什么AJAX对跨站脚本攻击必须谨慎,否则攻击者可以迫使人们执行AJAX命令你的网站,包括一个图像的url)

#6


-7  

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

为什么它重要吗?这是不应该的。你真的在做内容协商吗?

#1


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".

根据专业的ASP。MVC AJAX库将插入一个名为“X-Requested-With”的表单字段,值为“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.

然后可以在System.Web中使用扩展方法。这意味着您可以简单地调用request. isajaxrequest(),并得到一个简单的true或false,说明这是否是AJAX请求。

#2


7  

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:

或者当你在ASP的控制器中。它引用System.Web。Mvc,你会在HttpRequestBase对象上得到一个扩展方法,你可以在ActionMethod中访问它,比如:

bool isAjaxRequest = Request.IsAjaxRequest();

#3


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.

由于这是一个常规的http调用,因此没有特定的方法来确定调用是由javascript完成的还是直接在浏览器中完成的。

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.

您可以向ajax调用添加一个头,以将其与其他调用区分开来,也可以向仅用于ajax调用的查询字符串添加一个参数。

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.

ASP。NET MVC ajax确实添加了这样一个header - X-Requested-With: XMLHttpRequest,您可以使用它来嗅探这是MVC ajax库的ajax调用。但是,如果您正在使用jQuery或您自己的手工滚动的ajax调用,则不会设置这个参数。

#4


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:

检查请求是否是ajax请求的最好方法是检查request . isajaxrequest()。很高兴知道,在后台,MVC框架检查请求参数或请求头中的ajax请求。ASP中的代码。Net MVC源代码是:

    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.

所以如果你想要用人工检查(这是不推荐的),你必须同时检查这两个。

#5


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)

您需要传递一些参数与您的AJAX调用AJAX只是一个GET请求,然后在地址栏输入一个url没有什么不同和迫切的进入(这就是为什么AJAX对跨站脚本攻击必须谨慎,否则攻击者可以迫使人们执行AJAX命令你的网站,包括一个图像的url)

#6


-7  

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

为什么它重要吗?这是不应该的。你真的在做内容协商吗?