jQuery 如何在代码隐藏中检查请求是否为 ajax - ASP.NET Webforms
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4392836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if request is ajax or not in codebehind - ASP.NET Webforms
提问by DotnetDude
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#?
我试过了,Request.IsAjaxRequest
但这在 WebForms 中不存在。我正在进行 JQuery ajax 调用。如何在 C# 中检查这是否是 ajax 请求?
回答by Charlino
You could create your own extension method much like the one in the MVC code
您可以创建自己的扩展方法,就像MVC 代码中的方法一样
E.g.
例如
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,
Charles
HTH,
查尔斯
Edit:Actually Callback requests are also ajax requests,
编辑:其实回调请求也是ajax请求,
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");
}
回答by Tim Schmelter
Try to check if the ScriptManager IsInAsyncPostBack:
尝试检查 ScriptManager IsInAsyncPostBack 是否:
ScriptManager.GetCurrent(Page).IsInAsyncPostBack
回答by karim79
Generally, you will need to test for the X-Requested-With
header, ensuring that its value is 'XMLHttpRequest'. I'm not a C# developer (yet), but a quick google search says that in C# it goes something like this:
通常,您需要测试X-Requested-With
标头,确保其值为“XMLHttpRequest”。我还不是 C# 开发人员(还),但是在 google 上快速搜索说在 C# 中它是这样的:
Request.Headers["X-Requested-With"] == "XMLHttpRequest";
回答by devrooms
Yes, Request.IsAjaxRequest
looks at the headers and the querystring for X-Requested-With
, but it seems your jquery isn't sending the X-Requested-With
header.
是的, Request.IsAjaxRequest
查看 的标题和查询字符串X-Requested-With
,但您的 jquery 似乎没有发送X-Requested-With
标题。
You can try and see what headers it is sending by using Fiddler, or alternatively just send it in the querystring by setting the POST url to
您可以尝试使用 Fiddler 查看它发送的标头,或者通过将 POST url 设置为将其发送到查询字符串中
/whatever.aspx?x-requested-with=XMLHttpRequest
/whatever.aspx?x-requested-with=XMLHttpRequest
回答by JB's
Decorate your class with [WebMethod(EnableSession = true)]
syntax like if you write the following function in code behind and call the same function from ajax call you will be sure.
使用[WebMethod(EnableSession = true)]
语法装饰您的类,就像您在后面的代码中编写以下函数并从 ajax 调用中调用相同的函数一样,您将确定。
[WebMethod(EnableSession = true)]
public static void getData(string JSONFirstData,string JSONSecondData, string JSONThirdData, string JSONForthData, ...)
{
//code
}
in Ajax URL be like URL :'/Codebehind.aspx/getData'
在 Ajax URL 中就像 URL :'/Codebehind.aspx/getData'
回答by Cristian Oprea
I created an extension that I use:
我创建了一个我使用的扩展:
internal static bool IsAjaxRequest(this HttpRequestMessage request)
{
return request != null && request.Headers.Any(h => h.Key.Equals("X-Requested-With", StringComparison.CurrentCultureIgnoreCase) &&
h.Value.Any(v => v.Equals("XMLHttpRequest", StringComparison.CurrentCultureIgnoreCase)));
}