代码改变世界

更加高效调试 ASP.NET Ajax 应用程序

2009-03-31 02:18 by 晓风残月, ... 阅读, ... 评论, 收藏, 编辑

启用 AJAX 的 ASP.NET 应用程序如果发生抛出异常,默认情况下,客户端会报脚本错误,但错误信息仅包含异常的Message而没有重要的堆栈跟踪信息,很难以快速调试,这里总结一种自己平常使用的调试方法,发生错误时候,客户端会打印出详细的堆栈信息,有助于快速诊断,并容易在DEBUG 与 RELEASE 版本中切换。

<%@ Page Language="C#" Debug="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    
protected void Button1_Click(object sender, EventArgs e)
    
{
        Label1.Text 
= (int.Parse(TextBox1.Text) / int.Parse(TextBox2.Text)).ToString();
    }


    
protected void ScriptManager1_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)
    
{

#if DEBUG   // 假如 Debug="false" 仅输出 Message
        ScriptManager1.AsyncPostBackErrorMessage 
= e.Exception.ToString();
#else
        ScriptManager1.AsyncPostBackErrorMessage 
= e.Exception.Message;
#endif
        
//
        Server.ClearError();
    }

    
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    
<title></title>
</head>
<body>
    
<form id="form1" runat="server">
    
<asp:ScriptManager ID="ScriptManager1" runat="server" OnAsyncPostBackError="ScriptManager1_AsyncPostBackError">
    
</asp:ScriptManager>
    
<div>
        
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
            
<ContentTemplate>
                
<asp:TextBox ID="TextBox1" runat="server">1</asp:TextBox>/<asp:TextBox ID="TextBox2"
                    runat
="server">0</asp:TextBox>=<asp:Label ID="Label1" runat="server" Text=""></asp:Label><asp:Button
                        ID
="Button1" runat="server" Text="Divide" OnClick="Button1_Click" />
            
</ContentTemplate>
        
</asp:UpdatePanel>
    
</div>
    
<script type="text/javascript" language="javascript">
        
// error handler for async post back
        try { Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler); } catch (e) { }
        
//
        function endRequestHandler(sender, args) {
            
if (args.get_error() != undefined && args.get_error().httpStatusCode == '500'{
                var errorMessage 
= args.get_error().message
                args.set_errorHandled(
true);
                
try {
                
<% #if DEBUG  %>
                    showError(errorMessage);          
                
<% #else  %>
                   alert(errorMessage);       
                
<% #endif %>          
                }
 catch (e) {
                    alert(e.message);
                }

            }

        }

        
//
        function showError(error) {
            var win 
= window.open("about:blank");
            var doc 
= win.document;
            doc.write(
"<html><head><title>Errro Console: Sys.WebForms.PageRequestManagerServerErrorException</title>");
            doc.write(
"</head><body><div>");
            doc.write(String.format(
"<pre>{0}</pre>", [error]));
            doc.write(
"<br/>");
            doc.write(
"Occurs at " + (new Date()).toLocaleString() + "&nbsp;");
            doc.write(
"<span id='spnAutoClose'>Automatically close this page in <span id='spnAutoCloseTimer' style='width:48px'></span> seconds.</span>");
            doc.write(
"<input type='button' value='disable auto close' onclick='window.clearTimeout(autoCloseId);window.clearInterval(autoCloseTimerId);document.getElementById(\"spnAutoClose\").innerHTML=\"\";this.style.display=\"none\";' />");
            doc.write(
"<input type='button' value='close' onclick='window.close()' />");
            doc.write(
"</div>");
            doc.write(
"<script type='text/javascript'>");
            doc.write(
"var autoCloseSeconds = 60*1000;");
            doc.write(
"var autoCloseId = window.setTimeout('window.close()', autoCloseSeconds);");
            doc.write(
"var autoCloseTimerId = window.setInterval('autoCloseSeconds-=15;document.getElementById(\"spnAutoCloseTimer\").innerHTML = autoCloseSeconds/1000.0', 1);");
            doc.write(
"<" + "/script>");
            doc.close();
        }

    
</script>

    
</form>
</body>
</html>

原载:更加高效调试 ASP.NET Ajax 应用程序 http://digdotnet.com/post/aspnet-ajax-debug-effective.aspx

相关:更加高效调试 ASP.NET Ajax 应用程序http://iamcto.com/post/aspnet-ajax-debug-effective.aspx