Click to Rate and Give Feedback
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
Visual Studio Debugger
Walkthrough: Debugging a Web Form

This topic applies to:

Edition

Visual Basic

C#

C++

Web Developer

Express

Topic does not applyTopic does not applyTopic does not applyTopic does not apply

Standard

Topic appliesTopic appliesTopic does not applyTopic applies

Pro and Team

Topic appliesTopic appliesTopic does not applyTopic applies

Table legend:

Topic applies

Applies

Topic does not apply

Does not apply

Topic applies but command hidden by default

Command or commands hidden by default.

The steps in this walkthrough show you how to debug an ASP.NET Web application, also known as a Web Form. It shows you how to start and stop execution, set breakpoints, and examine variables in the Watch window.

NoteNote:

To complete this walkthrough, you must have Administrator privileges on the server computer. By default, the ASP.NET process, aspnet_wp.exe or w3wp.exe, runs as an ASP.NET process. To debug ASP.NET, you must have Administrator privileges on the computer where ASP.NET runs it. For more information, see ASP.NET Debugging: System Requirements.

The dialog boxes and menu commands you see might differ from those described in Help, depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

To create the Web Form

  1. If you already have an solution open, close it.

  2. On the File menu, click New, and then click Web Site.

    The New Web Site dialog box appears.

  3. In the Templates pane, click ASP.NET Web Site.

  4. On the Location line, click HTTP from the list, and in the text box, type http://localhost/WebSite.

  5. In the Language list, click Visual C# or Visual Basic.

  6. Click OK.

    Visual Studio creates a new project, and displays the default HTML source code. It also creates a new virtual directory named WebSite under Default Web Site in IIS.

  7. Click the Design tab on the bottom margin.

  8. Click the Toolbox tab on the left margin, or select it on the View menu.

    The Toolbox opens.

  9. In the Toolbox, click the Button control and add it to the main design surface, Default.aspx.

  10. In the Toolbox, click the Textbox control, and drag the control to the main design surface, Default.aspx.

  11. Double-click the button control you dropped.

    This takes you to the code page: Default.aspx.cs for C# or Default.aspx.vb for Visual Basic. The cursor should be in the function Button1_Click.

  12. In the Button1_Click function, add the following code:

    ' Visual Basic
    TextBox1.Text = "Button was clicked!"
    
    // C#
    TextBox1.Text = "Button was clicked!";
    
  13. On the Build menu, click Build Solution.

    The project should build without errors.

    Now, you are ready to start debugging.

To debug the Web Form

  1. In the Default.aspx.cs or Default.aspx.vb window, click the left margin on the same line as the text you added:

    ' Visual Basic
    TextBox1.Text = "Button was clicked!"
    
    // C#
    textBox1.Text = "Button was clicked!";
    

    A red dot appears and the text on the line is highlighted in red. The red dot represents a breakpoint. When you run the application under the debugger, the debugger will break execution at that location when the code is hit. You can then view the state of your application and debug it. For more information, see Breakpoints.

  2. On the Debug menu, click Start Debugging.

  3. The Debugging Not Enabled dialog box appears. Select Add a new Web.config file with debugging enabled option, and click OK.

    Internet Explorer starts and displays the page that you just designed.

  4. In Internet Explorer, click the button.

    In Visual Studio, this takes you to the line where you set your breakpoint on the code page Default.aspx.cs or Default.aspx.vb. This line should be highlighted in yellow. You can now view the variables in your application and control its execution. Your application stops executing and waits for a command from you.

  5. On the Debug menu, click Windows, then click Watch, and then click Watch1.

  6. In the Watch window, type TextBox1.Text.

    The Watch window shows the value of the variable TextBox1.Text:

    ""
    
  7. On the Debug menu, click Step Over.

    The value of TextBox1.Text changes in the Watch window to read:

    "Button was clicked!"
    
  8. On the Debug menu, click Continue.

  9. In Internet Explorer, click the button again.

    Execution stops at the breakpoint again.

  10. In the Default.aspx.cs or Default.aspx.vb window, click the red dot in the left margin.

    This removes the breakpoint.

  11. On the Debug menu, click Stop Debugging.

To attach to the Web Form for debugging

  1. In Visual Studio, you can attach the debugger to a running process. For most effective debugging, compile the executable as a Debug version with symbol (PDB) files.

  2. In the Default.aspx.cs or Default.aspx.vb window, click in the left margin to again set a breakpoint at the line you added:

    ' Visual Basic
    TextBox1.Text = "Button was clicked!"
    
    // C#
    textBox1.Text = "Button was clicked!";
    
  3. On the Debug menu, click Start Without Debugging.

    The Web Form starts to run under Internet Explorer, but the debugger is not attached.

  4. Attach to the ASP.NET process. For more information, see Debugging Deployed Web Applications and Web Services.

  5. In Internet Explorer, click the button on your form.

    In Visual Studio, you should hit the breakpoint in Default.aspx.cs, Default.aspx.vb, or Default.aspx.

  6. When you are finished debugging, on the Debug menu, click Stop Debugging.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Tip: use the debugger to see how your code is behaving      CSharpUniversity.com   |   Edit   |   Show History

While the debugger is great for determining the source of problems in your code, it can also be used to walk through your more complex or unfamiliar source code. For example if you have written a complex event handler and need to see exactly what the code is doing during runtime, run the debugger and walk through the code line by line. Try different inputs or test cases to work through as many paths in the code as possible. This also works great in situations where you have been placed with the responsibility of maintaining or extending code that somebody else has written. If you are unfamiliar with a piece of code, the debugger can help you analyze it.

To practice and learn more, go to: http://www.csharpuniversity.com/2008/11/23/using-the-visual-web-developer-debugger/

Tags What's this?: Add a tag
Flag as ContentBug
Web Server Debugging Comprehensive Help      Jeff Fischer ... Thomas Lee   |   Edit   |   Show History

Refer to my blog for helpful advice on Unable to start debugging on the web server.

Jeff Fischer

Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker