【问题标题】:debug c# webmethod from ajax call?从ajax调用调试c#webmethod?
【发布时间】:2016-08-18 15:55:05
【问题描述】:

我在页面后面有一个 asp.net(网络表单)c# 代码,它有一个网络方法,我从同一页面上的 ajax 调用中调用该方法。

在 ajax POST 调用之后,我收到一个通用的“发生内部服务器错误”。

我想单步调试 web 方法代码以确定发生了什么。

我该怎么做?没有命中断点。有我必须附加的过程吗?我找不到有关在处理 WebMethod 调用的本地 IIS 上运行什么进程的任何信息?

注意:我可以调试和单步调试正常的事件响应函数,例如Page_Load 等。

ajax 代码:

 $.ajax({
    type: "POST",
    url: "/Patrol/Report.aspx/GetSPResults",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: JSON.stringify(postData)
}).done(function (result) {
    var results = "";
    if (result.d.length > 0) {
        results = JSON.parse(result.d);
    }
    buildViewModel(results);
    kendo.bind($("#Report"), viewModel);
    $("Checkpoints").fadeIn(100);
}).fail(function (jqXHR, textStatus, err) {
    alert("An error has occurred: " + err);
});

网络方法:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string GetSPResults(string reportId, string sproc, Dictionary<string, string> parameters, string[] ensureArrays)
{
    Int32 intReportId;
    Console.WriteLine("Report.aspx/GetSPResults called");
    if (Int32.TryParse(reportId, out intReportId))
    {
        AV.Data.SqlSPHelper avdata = new AV.Data.SqlSPHelper(ConfigurationManager.ConnectionStrings["ProductionConnectionString"].ConnectionString);
        Dictionary<string, string> sprocCheckParams = new Dictionary<string, string>()
        {
            {"Sproc", sproc},
            {"ReportID", reportId}
        };
        XElement sprocCheck = avdata.ExecuteSPXmlXElement("[dw].[spReport_Sproc_Check]", sprocCheckParams, null);
        if (sprocCheck.GetAttributeVal("result") == "1")
        {
            XElement result = avdata.ExecuteSPXmlXElement(sproc, parameters, null);
            result = result?.RemoveAllNamespacesAndNils();
            if (result != null)
            {
                var jsonResults = JsonConvertWrapper.SerializeXelement(result, ensureArrays, true, Formatting.Indented, true);
                return jsonResults;
            }
        }
    }
    return string.Empty;
}

【问题讨论】:

  • 此代码是否有关联的.aspx 文件..?如果是这样,您是否有指向您的.cs 文件的CodeBehind 属性.. 你能显示.aspx 页面的标题吗..?
  • @MethodMan 我可以为你做这件事,但我可以在正常的事件函数上成功地逐步完成背后的代码,例如Page_Load 这会揭穿你的理论还是你仍然想要看到了吗?
  • 也许你发生了一些页面错误......根据你发布的代码很难判断发生了什么......
  • @MethodMan 我认为页面错误会出现一个有意义的 .NET 错误。不确定服务器错误消息是什么意思,这是我第一次使用 C# web 方法
  • @madasunaga 我想你误解了我的意思。 MethodMan 建议也许我的代码隐藏页面没有正确配置为与我的 aspx 页面相关联,我正在回复我通过Page_Load 的证据,因此它是正确附加的。这与直接调用ajax无关

标签: c# jquery asp.net ajax


【解决方案1】:

好的,问题是我的 post 请求的参数名称与 WebMethod 函数签名中的参数名称不匹配。

当 WebMethod 预期 reportId 时,POST 请求的参数名称为 reportID

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多