How To Debug an ASP.NET Web Application

How To Debug an ASP.NET Web Application

适用于
This article was previously published under Q316726
For additional information about how to perform this task by using Microsoft Active Server Pages, click the article number below to view the article in the Microsoft Knowledge Base:

299986 How To Use an ASP Debug Object to Debug ASP Pages

IN THIS TASK

SUMMARY

Use this step-by-step guide to set up a breakpoint, use page-level tracing, and write out custom trace messages in an ASP.NET application.

The new debugging features in ASP.NET allow you to track progress through your Web applications and more easily identify and diagnose problems.

back to the top

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
  • Microsoft Visual Studio .NET.
  • Microsoft Internet Information Services (IIS) 5.0.
This article assumes that you are familiar with the following topics:
  • Web applications.
  • Microsoft ASP.NET.
back to the top

Overview

Debugging a traditional ASP application generally involves placing Response.Write statements throughout your code to track variable values and execution paths. If you fail to remove debugging statements before you deploy your application, the statements are visible to users.

ASP.NET makes Web application debugging much simpler to use. You can use tracing statements to debug your application in a way that is similar to using Response.Write statements. However, you can leave the tracing statements in your application, even after deployment.

You can configure the Web application to allow tracing at either the page level or the application level, and you can easily turn it on and off. You can also control the destination of the tracing output to allow only certain users to see the debugging output.

back to the top
Create an ASP.NET Application and Add Breakpoints to Debug the Application
The following procedure creates a simple ASP.NET Web application that performs a calculation on two numbers and displays the results on the screen. When you add a breakpoint to the Add function, notice how the new interactive debugging capabilities of ASP.NET behave:
  1. Start Microsoft Visual Studio .NET.
  2. Create a new Visual Basic ASP.NET Web Application project and name it ASPDebuggingExample.
  3. In WebForm1.aspx, switch to HTML view.
  4. Type or paste the following code between the opening and closing FORM tags.

    NOTE: If you paste the code, paste it as HTML code. To do this, click Paste as HTML on the Edit menu.

    When you run the code, it prompts you to type two numbers and displays a command button. When you click the command button, the code adds the two numbers and displays the result on your screen.
    <TABLE id="Table1" cellSpacing="1" cellPadding="1" width="300" border="0">
    
    <TR><TD>
    <asp:Label id="Label1" runat="server">First Number:</asp:Label>
    </TD>
    <TD>
    <asp:TextBox id="txtFirstNumber" runat="server"></asp:TextBox>
    </TD></TR>
    <TR><TD>
    <asp:Label id="Label2" runat="server">Second Number:</asp:Label>
    </TD>
    <TD>
    <asp:TextBox id="txtSecondNumber" runat="server"></asp:TextBox>
    </TD></TR>
    <TR><TD>
    <asp:Label id="Label3" runat="server">Sum:</asp:Label>
    </TD>
    <TD>
    <asp:TextBox id="txtSum" runat="server"></asp:TextBox>
    </TD></TR>
    <TR><TD>
    <asp:Button id="Button1" runat="server" Text="Add Numbers"></asp:Button>
    </TD>
    <TD>
    </TD></TR>
    </TABLE>
    					
  5. Click the Design button in the lower-left corner of the Code window to switch to Design view.
  6. Double-click Add Numbers to view the code and write an event handler for the command button's Click event.
  7. Add the following code to the event handler:
    Dim intTotal as integer
    Dim intFirstNumber as integer
    Dim intSecondNumber as integer
    			
    ' Get the values from the input boxes.
    intFirstNumber = CInt(txtFirstNumber.Text)
    intSecondNumber = CInt(txtSecondNumber.Text)
    						
    ' Get the total and display it.
    intTotal = intFirstNumber + intSecondNumber
    txtSum.Text = CStr(intTotal)
    					
  8. Select the following line of code in the Button1_Click routine:
    intTotal = intFirstNumber + intSecondNumber
    						
    Press F9 to set a breakpoint on this line.

    A breakpoint indicator appears on the line of code.
  9. Save the file.
  10. Build and run the application. To do this, click Start on the Debug menu.

    WebForm1 appears on the screen. The input controls and the submit button also appear on the form.
  11. Type a number in each of the first two input controls and then click Add Number.

    The Visual Studio .NET Integrated Development Environment (IDE) comes into focus and the code halts when the breakpoint is reached. You can use the Locals window to examine the contents of the variables and make any changes you want. You can add one or more watches to trace the values of the variables in the routine. Or, you can continue running the application.
  12. On the Debug menu, click Continue to continue running the application.

    WebForm1 appears on the screen and the result appears in the third input box.
  13. Close the Web browser and return to the Visual Studio .NET IDE.
  14. On the Debug menu, click Clear All Breakpoints to remove all breakpoints from the application.
back to the top
Verify it Works
When you click Add Number, the Visual Studio .NET IDE comes into focus and the code stops running at the point where the breakpoint is specified.

back to the top
Putting It All Together
&amp;lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;&amp;gt;
&amp;lt;html&amp;gt;
	&amp;lt;head&amp;gt;
		&amp;lt;title&amp;gt;WebForm1&amp;lt;/title&amp;gt;
		&amp;lt;meta name=&quot;GENERATOR&quot; content=&quot;Microsoft Visual Studio.NET 7.0&quot;&amp;gt;
		&amp;lt;meta name=&quot;CODE_LANGUAGE&quot; content=&quot;Visual Basic 7.0&quot;&amp;gt;
		&amp;lt;meta name=&quot;vs_defaultClientScript&quot; content=&quot;JavaScript&quot;&amp;gt;
		&amp;lt;meta name=&quot;vs_targetSchema&quot; content=&quot;http://schemas.microsoft.com/intellisense/ie5&quot;&amp;gt;
	&amp;lt;/head&amp;gt;
	&amp;lt;Script language=&quot;vb&quot; runat=&quot;server&quot; id=&quot;script1&quot;&amp;gt;
		Sub Button1_Click(Sender as object, e as EventArgs)
			Dim intTotal as integer
			Dim intFirstNumber as integer
			Dim intSecondNumber as integer
			
			&apos; Get the values from the input boxes.
			intFirstNumber = CInt(txtFirstNumber.value)
			intSecondNumber = CInt(txtSecondNumber.value)
			
			&apos; Get the total and display it.
			intTotal = intFirstNumber + intSecondNumber
			txtTotal.value = CStr(intTotal)	
			
		End Sub
	&amp;lt;/Script&amp;gt;
	&amp;lt;body MS_POSITIONING=&quot;GridLayout&quot;&amp;gt;
		&amp;lt;form id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;&amp;gt;
			&amp;lt;asp:Label id=&quot;lblFirstNumber&quot; runat=server Width=125&amp;gt;
				First Number:
			&amp;lt;/asp:Label&amp;gt; 
			&amp;lt;input id=txtFirstNumber type=text size=5 maxlength=3 
				runat=server NAME=&quot;txtFirstNumber&quot;/&amp;gt;
			&amp;lt;br&amp;gt;
			&amp;lt;asp:Label id=&quot;lblSecondNumber&quot; runat=server Width=125&amp;gt;
				Second Number:
			&amp;lt;/asp:Label&amp;gt; 
			&amp;lt;input id=txtSecondNumber type=text size=5 maxlength=3 
				runat=server NAME=&quot;txtSecondNumber&quot;/&amp;gt;
			&amp;lt;br&amp;gt;
			&amp;lt;asp:Label id=&quot;lblTotal&quot; runat=server Width=125&amp;gt;
				Total:
			&amp;lt;/asp:Label&amp;gt; 
			&amp;lt;input id=&quot;txtTotal&quot; type=text size=5 maxlength=5 
				runat=server NAME=&quot;txtTotal&quot; readonly/&amp;gt;
			&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;
			&amp;lt;input type=submit id=Button1 value=&quot;Add Numbers&quot; onserverclick=&quot;Button1_Click&quot; 
				runat=server NAME=&quot;Button1&quot;/&amp;gt;
		&amp;lt;/form&amp;gt;
	&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
		"?><%@ Page Language="vb"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
	<HEAD>
		<title>WebForm1</title>
		<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
		<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
		<meta name="vs_defaultClientScript" content="JavaScript">
		<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
		<script language="vb" runat="server" id="script1">
		Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim intTotal As Integer
        Dim intFirstNumber As Integer
        Dim intSecondNumber As Integer

        ' Get the values from the input boxes.
        intFirstNumber = CInt(txtFirstNumber.Text)
        intSecondNumber = CInt(txtSecondNumber.Text)

        ' Get the total and display it.
        intTotal = intFirstNumber + intSecondNumber      
        txtSum.Text = CStr(intTotal)

    End Sub
		</script>
	</HEAD>
	<body MS_POSITIONING="GridLayout">
		<form id="Form1" method="post" runat="server">
			<TABLE id="Table1" cellSpacing="1" cellPadding="1" width="300" border="0">
				<TR>
					<TD style="HEIGHT: 25px">
						<asp:Label id="Label1" runat="server">First Number:</asp:Label>
					</TD>
					<TD style="HEIGHT: 25px">
						<asp:TextBox id="txtFirstNumber" runat="server"></asp:TextBox>
					</TD>
				</TR>
				<TR>
					<TD>
						<asp:Label id="Label2" runat="server">Second Number:</asp:Label>
					</TD>
					<TD>
						<asp:TextBox id="txtSecondNumber" runat="server"></asp:TextBox>
					</TD>
				</TR>
				<TR>
					<TD>
						<asp:Label id="Label3" runat="server">Sum:</asp:Label>
					</TD>
					<TD>
						<asp:TextBox id="txtSum" runat="server"></asp:TextBox>
					</TD>
				</TR>
				<TR>
					<TD>
						<asp:Button id="Button1" runat="server" Text="Add Numbers" OnClick="Button1_Click"></asp:Button>
					</TD>
					<TD>
					</TD>
				</TR>
			</TABLE>
		</form>
	</body>
</HTML>

back to the top
Add Page-Level Tracing to the ASP.NET Application
The following procedure sets up the Web application to allow the tracing output to be displayed. It adds the page level Trace directive to the page to indicate that page-level tracing is being used. The tracing code throughout the page tracks execution and the variable contents:
  1. In WebForm1.aspx, switch to HTML view. Add the following attribute-value pair to the page directive at the top of the Code window:
    Trace=True
    					
  2. Right-click WebForm1.aspx and then click View Code on the shortcut menu.
  3. In the Button1_Click routine, use trace statements to display the contents of variables at several different points in the routine.

    For example, replace the existing routine with the following:
    Trace.Write("Button1_Click", "Entering the Add routine")
    			
    Dim intTotal as integer
    Dim intFirstNumber as integer
    Dim intSecondNumber as integer
    			
    ' Get values from the input boxes.
    intFirstNumber = CInt(txtFirstNumber.Text)
    intSecondNumber = CInt(txtSecondNumber.Text)
    Trace.Write("Button1_Click", "Amount to add:" & _
    CStr(intFirstNumber) & " and " & CStr(intSecondNumber))
    			
    ' Get the total and display it.
    intTotal = intFirstNumber + intSecondNumber
    Trace.Write("Button1_Click", "Total:" & CStr(intTotal))
    txtSum.Text = CStr(intTotal)	
    			
    Trace.Write("Button1_Click", "Leaving the Add routine")
    					
  4. Click Save.
  5. On the Debug menu, click Start to build and run the application.

    WebForm1 appears on the screen. Notice the tracing information for the application, such as request information, tracing information, the hierarchical listing of controls on the page, and any persistent items in the Application or Session states.
  6. Type numbers into the first two input controls and then click Add Number.

    The result appears in the third input box. And, the custom trace messages from the Button1_Click routine appear in the Trace Information section.
back to the top

Verify it Works


The first time the form loads, general tracing information for the page is displayed. If you click the Add Number button, the custom trace messages from the Button1_Click routine appear in the Trace Information section.

back to the top
Putting It All Together

&amp;lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;&amp;gt;
&amp;lt;html&amp;gt;
	&amp;lt;head&amp;gt;
		&amp;lt;title&amp;gt;WebForm1&amp;lt;/title&amp;gt;
		&amp;lt;meta name=&quot;GENERATOR&quot; content=&quot;Microsoft Visual Studio.NET 7.0&quot;&amp;gt;
		&amp;lt;meta name=&quot;CODE_LANGUAGE&quot; content=&quot;Visual Basic 7.0&quot;&amp;gt;
		&amp;lt;meta name=&quot;vs_defaultClientScript&quot; content=&quot;JavaScript&quot;&amp;gt;
		&amp;lt;meta name=&quot;vs_targetSchema&quot; content=&quot;http://schemas.microsoft.com/intellisense/ie5&quot;&amp;gt;
	&amp;lt;/head&amp;gt;
	&amp;lt;Script language=&quot;vb&quot; runat=&quot;server&quot; id=&quot;script1&quot;&amp;gt;
		Sub Button1_Click(Sender as object, e as EventArgs)
			Trace.Write(&quot;Button1_Click&quot;, &quot;Entering the Add routine&quot;)
			
			Dim intTotal as integer
			Dim intFirstNumber as integer
			Dim intSecondNumber as integer
			
			&apos; Get the values from the input boxes.
			intFirstNumber = CInt(txtFirstNumber.value)
			intSecondNumber = CInt(txtSecondNumber.value)
			Trace.Write(&quot;Button_Click&quot;, &quot;Amount to add:&quot; &amp;amp; CStr(intFirstNumber) &amp;amp; &quot; and &quot; &amp;amp; CStr(intSecondNumber))
			
			&apos; Get the total and display it.
			intTotal = intFirstNumber + intSecondNumber
			Trace.Write(&quot;Button1_Click&quot;, &quot;Total:&quot; &amp;amp; CStr(intTotal))
			txtTotal.value = CStr(intTotal)	
			
			Trace.Write(&quot;Button1_Click&quot;, &quot;Leaving the Add routine&quot;)
		End Sub
	&amp;lt;/Script&amp;gt;
	&amp;lt;body MS_POSITIONING=&quot;GridLayout&quot;&amp;gt;
		&amp;lt;form id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;&amp;gt;
			&amp;lt;asp:Label id=&quot;lblFirstNumber&quot; runat=server Width=125&amp;gt;
				First Number:
			&amp;lt;/asp:Label&amp;gt; 
			&amp;lt;input id=txtFirstNumber type=text size=5 maxlength=3 
				runat=server NAME=&quot;txtFirstNumber&quot;/&amp;gt;
			&amp;lt;br&amp;gt;
			&amp;lt;asp:Label id=&quot;lblSecondNumber&quot; runat=server Width=125&amp;gt;
				Second Number:
			&amp;lt;/asp:Label&amp;gt; 
			&amp;lt;input id=txtSecondNumber type=text size=5 maxlength=3 
				runat=server NAME=&quot;txtSecondNumber&quot;/&amp;gt;
			&amp;lt;br&amp;gt;
			&amp;lt;asp:Label id=&quot;lblTotal&quot; runat=server Width=125&amp;gt;
				Total:
			&amp;lt;/asp:Label&amp;gt; 
			&amp;lt;input id=&quot;txtTotal&quot; type=text size=5 maxlength=5 
				runat=server NAME=&quot;txtTotal&quot; readonly/&amp;gt;
			&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;
			&amp;lt;input type=submit id=Button1 value=&quot;Add Numbers&quot; onserverclick=&quot;Button1_Click&quot; 
				runat=server NAME=&quot;Button1&quot;/&amp;gt;
		&amp;lt;/form&amp;gt;
	&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
		"?><%@ Page Language="vb" Trace=true%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
	<HEAD>
		<title>WebForm1</title>
		<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
		<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
		<meta name="vs_defaultClientScript" content="JavaScript">
		<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
		<Script language="vb" runat="server" id="script1">
		Sub Button1_Click(Sender as object, e as EventArgs)
		
            Trace.Write("Button1_Click", "Entering the Add routine")			
            Dim intTotal as integer
            Dim intFirstNumber as integer
            Dim intSecondNumber as integer
            			
            ' Get values from the input boxes.
            intFirstNumber = CInt(txtFirstNumber.Text)
            intSecondNumber = CInt(txtSecondNumber.Text)
            Trace.Write("Button1_Click", "Amount to add:" & _
            CStr(intFirstNumber) & " and " & CStr(intSecondNumber))
            			
            ' Get the total and display it.
            intTotal = intFirstNumber + intSecondNumber
            Trace.Write("Button1_Click", "Total:" & CStr(intTotal))
            txtSum.Text = CStr(intTotal)	
            			
            Trace.Write("Button1_Click", "Leaving the Add routine")

		End Sub
		</Script>
	</HEAD>
	<body MS_POSITIONING="GridLayout">
		<form id="Form1" method="post" runat="server">
			<TABLE id="Table1" cellSpacing="1" cellPadding="1" width="300" border="0">
				<TR>
					<TD><asp:Label id="Label1" runat="server">First Number:</asp:Label>
					</TD>
					<TD>
						<asp:TextBox id="txtFirstNumber" runat="server"></asp:TextBox>
					</TD>
				</TR>
				<TR>
					<TD>
						<asp:Label id="Label2" runat="server">Second Number:</asp:Label>
					</TD>
					<TD>
						<asp:TextBox id="txtSecondNumber" runat="server"></asp:TextBox>
					</TD>
				</TR>
				<TR>
					<TD>
						<asp:Label id="Label3" runat="server">Sum:</asp:Label>
					</TD>
					<TD>
						<asp:TextBox id="txtSum" runat="server"></asp:TextBox>
					</TD>
				</TR>
				<TR>
					<TD>
						<asp:Button id="Button1" runat="server" Text="Add Numbers" OnClick="Button1_Click"></asp:Button>
					</TD>
					<TD>
					</TD>
				</TR>
			</TABLE>
		</form>
	</body>
</HTML>

back to the top

REFERENCES

For additional information, see the following Microsoft Web sites:

Debugging ASP.NET Web Applications
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cptutorials/html/debugging_asp_net_web_applications.asp?frame=trueTracing
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspnet/html/asp01252001.aspEnabling Tracing for a Page

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:

306172 INFO: Common Errors When You Debug ASP.NET Applications in Visual Studio .NET

[Come from Microsoft Support]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值