如何检查是否请求阿贾克斯与否codebehind - ASP.NET Web表单 [英] How to check if request is ajax or not in codebehind - ASP.NET Webforms
本文介绍了如何检查是否请求阿贾克斯与否codebehind - ASP.NET Web表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试过 Request.IsAjaxRequest
但是,这并不在WebForms的存在。我想提出一个JQuery的AJAX调用。我如何检查,如果这是一个Ajax请求或不是在C#?
I tried the Request.IsAjaxRequest
but this does not exist in WebForms. I am making a JQuery ajax call. How do I check if this is a ajax request or not in C#?
推荐答案
您可以创建自己的扩展方法很像一个在的 MVC code
You could create your own extension method much like the one in the MVC code
例如。
public static bool IsAjaxRequest(this HttpRequest request)
{
if (request == null)
{
throw new ArgumentNullException("request");
}
return (request["X-Requested-With"] == "XMLHttpRequest") || ((request.Headers != null) && (request.Headers["X-Requested-With"] == "XMLHttpRequest"));
}
HTHS,
查尔斯
HTHs,
Charles
编辑:其实回调的要求也Ajax请求,
Actually Callback requests are also ajax requests,
public static bool IsAjaxRequest(this HttpRequest request)
{
if (request == null)
{
throw new ArgumentNullException("request");
}
var context = HttpContext.Current;
var isCallbackRequest = false;// callback requests are ajax requests
if (context != null && context.CurrentHandler != null && context.CurrentHandler is System.Web.UI.Page)
{
isCallbackRequest = ((System.Web.UI.Page)context.CurrentHandler).IsCallback;
}
return isCallbackRequest || (request["X-Requested-With"] == "XMLHttpRequest") || (request.Headers["X-Requested-With"] == "XMLHttpRequest");
}
这篇关于如何检查是否请求阿贾克斯与否codebehind - ASP.NET Web表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文