简体   繁体   English

自定义IsAjaxRequest函数在MVC 6中不起作用

[英]Custom IsAjaxRequest function doesn't work in MVC 6

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: 我在MVC 6控制器中检测到ajax请求时遇到了麻烦,因为在MVC 6中找不到IsAjaxRequest,所以我使用了以下功能:

        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 但是函数总是返回false

my jquery code: 我的jQuery代码:

            $("ul.menu a").click(function (e) {
            e.preventDefault();
            var url = $(this).attr("href");
            $('#main-container').load(url);
        });

问题出在选择器中,因为它是错误的e.preventDefault不起作用,请求正常进行,这就是为什么它永远不会是ajax请求的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM