How to check if request is ajax or not in codebehind - ASP.NET Webforms

后端 未结 6 1562
余生分开走
余生分开走 2020-12-05 04:26

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#?

6条回答
  •  醉梦人生
    2020-12-05 05:06

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

提交回复
热议问题