0

I'm having a trouble in detecting ajax request in MVC 6 controller, since IsAjaxRequest is not found in MVC 6 I used the following function:

        public static bool IsAjaxRequest(this HttpRequest request)
    {
        if (request == null)
            throw new ArgumentNullException(nameof(request));
        var x = request.Headers["X-Requested-With"];
        if (request.Headers != null)
            return request.Headers["X-Requested-With"] == "XMLHttpRequest";
        return false;
    }

but the function always return false

my jquery code:

            $("ul.menu a").click(function (e) {
            e.preventDefault();
            var url = $(this).attr("href");
            $('#main-container').load(url);
        });
Morz
  • 55
  • 1
  • 7
  • check [this](https://stackoverflow.com/questions/29282190/where-is-request-isajaxrequest-in-asp-net-core-mvc) may be useful – Mahesh Singh Chouhan Jun 02 '17 at 15:59
  • I've already done, but the method always return false. – Morz Jun 02 '17 at 16:06
  • Can you see from your browser if the request header contains X-Requested-With? Is the url you are loading from the same domain as the page that is loaded on the browser? – rino.batin Jun 05 '17 at 13:34

1 Answers1

0

The issue was in the selector, since it was wrong e.preventDefault doesn't work and the request continues normally, that's why it will never be ajax request.

Morz
  • 55
  • 1
  • 7