Search code examples
c#xmlhttprequestglobal-asaxhttpcontext

Is there a way to know if a System.Web.HttpRequest is a XMLHttpRequest from Global.asax?


in Global.asax.cs, I'm trying to filter out all the XHR requests in order to log them.

However, both the HttpContext.Current.Request and the HttpContext.Current.Response don't seem to provide this information.

Is there some property field I missed? Or is there another way of knowing if a request is XHR?


Solution

  • If you're using MVC you can use the IsAjaxRequest() extension method.

    Request.IsAjaxRequest();
    

    It's defined in System.Web.Mvc so if you don't already have a using statement for that namespace you'll need to add one.