Microsoft KB Archive/316726

= How to debug an ASP.NET Web application =

Article ID: 316726

Article Last Modified on 5/16/2007

-

APPLIES TO


 * Microsoft ASP.NET 1.1
 * Microsoft ASP.NET 1.0

-



This article was previously published under Q316726



For more information about how to perform this task by using Microsoft Active Server Pages, click the following article number to view the article in the Microsoft Knowledge Base:

299986 How to use an ASP debug object to debug ASP pages



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.

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

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.

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:  Start Microsoft Visual Studio .NET. Create a new Visual Basic ASP.NET Web Application project. Name it ASPDebuggingExample. In WebForm1.aspx, switch to HTML view.  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.  Click the Design button in the lower-left corner of the Code window to switch to Design view. Double-click Add Numbers to view the code and write an event handler for the command button's Click event.  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) </li>  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. </li> Save the file.</li> Build and then 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.</li> 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.</li> 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.</li> Close the Web browser and return to the Visual Studio .NET IDE.</li> On the Debug menu, click Clear All Breakpoints to remove all breakpoints from the application.</li></ol>

Verify that 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.

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: <ol>  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 </li> Right-click WebForm1.aspx, and then click View Code.</li>  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(&quot;Button1_Click&quot;, &quot;Entering the Add routine&quot;) 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(&quot;Button1_Click&quot;, &quot;Amount to add:&quot; & _ CStr(intFirstNumber) & &quot; and &quot; & CStr(intSecondNumber)) ' Get the total and display it. intTotal = intFirstNumber + intSecondNumber Trace.Write(&quot;Button1_Click&quot;, &quot;Total:&quot; & CStr(intTotal)) txtSum.Text = CStr(intTotal) Trace.Write(&quot;Button1_Click&quot;, &quot;Leaving the Add routine&quot;) </li> Click Save.</li> 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.</li> 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.</li></ol>

Verify that 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.

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

End Sub </Script> </HEAD> <body MS_POSITIONING=&quot;GridLayout&quot;> <form id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;> </HTML>

<div class="references_section">