我编写了一个快速的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返回,并且看起来它正在执行处理程序代码,只是没有停止在其中。任何建议都将受到欢迎。
答案 0 :(得分:2)
按照惯例,根据方案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;
}