【问题标题】:How to check if request is ajax or not in codebehind - ASP.NET Webforms如何在代码隐藏中检查请求是否为 ajax - ASP.NET Webforms
【发布时间】:2011-05-22 12:37:51
【问题描述】:

我尝试了Request.IsAjaxRequest,但这在 WebForms 中不存在。我正在做一个 JQuery ajax 调用。如何在 C# 中检查这是否是 ajax 请求?

【问题讨论】:

    标签: asp.net jquery ajax webforms


    【解决方案1】:

    我创建了一个我使用的扩展:

    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)));
    }
    

    【讨论】:

      【解决方案2】:

      [WebMethod(EnableSession = true)]syntax 装饰你的类,就像你在后面的代码中编写以下函数并从ajax 调用中调用相同的函数一样,你会确定。

      [WebMethod(EnableSession = true)]
          public static void   getData(string JSONFirstData,string JSONSecondData, string JSONThirdData, string JSONForthData, ...)
          {
             //code
          }
      

      在 Ajax URL 中就像 URL :'/Codebehind.aspx/getData'

      【讨论】:

        【解决方案3】:

        尝试检查ScriptManager是否IsInAsyncPostBack

        ScriptManager.GetCurrent(Page).IsInAsyncPostBack
        

        【讨论】:

        • 这是否适用于从 jquery ajax 以及更新面板等控件触发的 ajax 请求?
        • 我不确定,所以我写了try;-)
        【解决方案4】:

        您可以创建自己的扩展方法,就像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"));
        }
        

        HTH,
        查尔斯

        编辑:其实回调请求也是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");
            }
        

        【讨论】:

        • 最后一行可以重构为return request["X-Requested-With"] == "XMLHttpRequest" || request.Headers["X-Requested-With"] == "XMLHttpRequest";
        • 添加了回调支持
        【解决方案5】:

        是的,Request.IsAjaxRequest 会查看 X-Requested-With 的标头和查询字符串,但您的 jquery 似乎没有发送 X-Requested-With 标头。

        您可以尝试使用 Fiddler 查看它发送的标头,或者通过将 POST url 设置为

        将其发送到查询字符串中

        /whatever.aspx?x-requested-with=XMLHttpRequest

        【讨论】:

        • JQuery 正在发送 X-Requested-With 所以如果我检查每个 Karim79 的标头,它就可以工作。但是 Request 在基页中没有 IsAjaxRequest 属性。
        • 啊,我明白了——我以为你的意思是它总是错误的。您使用的是什么版本的 ASP.net,我们都可以看到一些代码来重现您的问题吗?
        【解决方案6】:

        通常,您需要测试X-Requested-With 标头,确保其值为“XMLHttpRequest”。我还不是 C# 开发人员(还),但快速的谷歌搜索表明在 C# 中它是这样的:

        Request.Headers["X-Requested-With"] == "XMLHttpRequest";
        

        【讨论】:

          猜你喜欢
          • 2017-02-16
          • 1970-01-01
          • 2021-06-25
          • 2010-11-14
          • 2014-05-06
          • 2015-05-27
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多