首页 > 解决方案 > AJAX 调用未命中异步处理程序中的断点

问题描述

我编写了一个快速 AJAX 脚本,以便在按钮按下事件上调用,该事件又调用 asysnc 处理程序以从远程 API 中提取数据。我修改了同一个脚本来调用另一个非异步的处理程序并且它工作正常,我不确定为什么它没有在 Visual Studio 中遇到断点。这是 AJAX 脚本。

$("#RunNewShodanQuery").click(function (d) {
    $.ajax(
        {
            type: "POST",
            async: true,
            url: "/Tools/Test?handler=UpdateResultsAsync",
            beforeSend: function (xhr) {
                xhr.setRequestHeader("XSRF-TOKEN",
                    $('input:hidden[name="__RequestVerificationToken"]').val());
            },
            error: function (jqXHR, exception) {
                var msg = '';
                if (jqXHR.status === 0) {
                    msg = 'Not connect.\n Verify Network.';
                } else if (jqXHR.status == 404) {
                    msg = 'Requested page not found. [404]';
                } else if (jqXHR.status == 500) {
                    msg = 'Internal Server Error [500].';
                } else if (exception === 'parsererror') {
                    msg = 'Requested JSON parse failed.';
                } else if (exception === 'timeout') {
                    msg = 'Time out error.';
                } else if (exception === 'abort') {
                    msg = 'Ajax request aborted.';
                } else {
                    msg = 'Uncaught Error.\n' + jqXHR.responseText;
                }
                console.log(msg);
            },
            complete: function (res) {
                console.log(res);
            }
        });
})

这是有问题的处理程序。

    public async Task OnPostUpdateResultsAsync()
    {
        ModelState.Clear();
        foreach (Entry e in _context.Entries)
        {
            // TBI
        }

        // Update Date of Last Scan so as not to make needless API calls spamming refreshes
        DateOfLastScan = DateTime.Now;

        // Dispose of the client once we're done
        client.Dispose();
    }

我在另一个测试处理程序中放置了断点,并使用 URL 修改了上面的 AJAX 以指向新的测试处理程序,并且 VS 在该处理程序内的断点处停止。

    public void OnPostTestHandler()
    {
        int seven = 5;
    }

我目前不知道为什么 Visual Studio 没有在异步处理程序中遇到断点。从浏览器中,我看到条目返回状态为 200,并且它似乎正在执行处理程序代码,只是没有在其中停止。任何建议都会非常受欢迎。

标签: ajaxasp.net-core-2.0razor-pages

解决方案


按照惯例,处理程序方法的名称是根据方案根据处理程序参数的值来选择的OnPost[handler]Async

这意味着,对于OnPostUpdateResultsAsync,处理程序名称是UpdateResults而不是UpdateResultsAsync

对于 Razor 页面,PageActionInvoker将调用DefaultPageHandlerMethodSelector.SelectHandlers以选择处理程序。

        private List<HandlerMethodDescriptor> SelectHandlers(PageContext context)
    {
        var handlers = context.ActionDescriptor.HandlerMethods;
        var candidates = new List<HandlerMethodDescriptor>();

        // Name is optional, may not be provided.
        var handlerName = GetHandlerName(context);

        // The handler selection process considers handlers according to a few criteria. Handlers
        // have a defined HTTP method that they handle, and also optionally a 'name'.
        //
        // We don't really have a scenario for handler methods without a verb (we don't provide a way
        // to create one). If we see one, it will just never match.
        //
        // The verb must match (with some fuzzy matching) and the handler name must match if
        // there is one.
        //
        // The process is like this:
        //
        //  1. Match the possible candidates on HTTP method
        //  1a. **Added in 2.1** if no candidates matched in 1, then do *fuzzy matching*
        //  2. Match the candidates from 1 or 1a on handler name.

        // Step 1: match on HTTP method.
        var httpMethod = context.HttpContext.Request.Method;
        for (var i = 0; i < handlers.Count; i++)
        {
            var handler = handlers[i];
            if (handler.HttpMethod != null &&
                string.Equals(handler.HttpMethod, httpMethod, StringComparison.OrdinalIgnoreCase))
            {
                candidates.Add(handler);
            }
        }

        // Step 1a: do fuzzy HTTP method matching if needed.
        if (candidates.Count == 0 && AllowFuzzyHttpMethodMatching)
        {
            var fuzzyHttpMethod = GetFuzzyMatchHttpMethod(context);
            if (fuzzyHttpMethod != null)
            {
                for (var i = 0; i < handlers.Count; i++)
                {
                    var handler = handlers[i];
                    if (handler.HttpMethod != null &&
                        string.Equals(handler.HttpMethod, fuzzyHttpMethod, StringComparison.OrdinalIgnoreCase))
                    {
                        candidates.Add(handler);
                    }
                }
            }
        }

        // Step 2: remove candidates with non-matching handlers.
        for (var i = candidates.Count - 1; i >= 0; i--)
        {
            var handler = candidates[i];
            if (handler.Name != null &&
                !handler.Name.Equals(handlerName, StringComparison.OrdinalIgnoreCase))
            {
                candidates.RemoveAt(i);
            }
        }

        return candidates;
    }

推荐阅读