"
ASP.NET (snapshot 2017) Microsoft documentation and samples

Introduction to Debugging ASP.NET Web Pages (Razor) Sites

by Tom FitzMacken

This article explains various ways to debug pages in an ASP.NET Web Pages (Razor) website. Debugging is the process of finding and fixing errors in your code pages.

What you’ll learn:

  • How to display information that helps analyze and debug pages.
  • How to use debugging tools in Visual Studio.

These are the ASP.NET features introduced in the article:

  • The ServerInfo helper.
  • ObjectInfo helper.

Software versions

  • ASP.NET Web Pages (Razor) 3
  • Visual Studio 2013

This tutorial also works with ASP.NET Web Pages 2. You can use WebMatrix 3 but the integrated debugger is not supported.

An important aspect of troubleshooting errors and problems in your code is to avoid them in the first place. You can do that by putting sections of your code that are likely to cause errors into try/catch blocks. For more information, see the section on handling errors in Introduction to ASP.NET Web Programming Using the Razor Syntax.

The ServerInfo helper is a diagnostic tool that gives you an overview of information about the web server environment that hosts your page. It also shows you HTTP request information that’s sent when a browser requests the page. The ServerInfo helper displays the current user identity, the type of browser that made the request, and so on. This kind of information can help you troubleshoot common issues.

  1. Create a new web page named ServerInfo.cshtml.
  2. At the end of the page, just before the closing </body> tag, add @ServerInfo.GetHtml():

    [!code-cshtmlMain]

       1:  <!DOCTYPE html>
       2:  <html>
       3:      <head>
       4:          <title></title>
       5:      </head>
       6:      <body>
       7:     @ServerInfo.GetHtml()
       8:      </body>
       9:  </html>

    You can add the ServerInfo code anywhere in the page. But adding it at the end will keep its output separate from your other page content, which makes it easier to read.

    [!NOTE]

    Important You should remove any diagnostic code from your web pages before you move web pages to a production server. This applies to the ServerInfo helper as well as the other diagnostic techniques in this article that involve adding code to a page. You don’t want your website visitors to see information about your server name, user names, paths on your server, and similar details, because this type of information might be useful to people with malicious intent.

  3. Save the page and run it in a browser.

    Debugging-1
    Debugging-1

    The ServerInfo helper displays four tables of information in the page:

    • Server Configuration. This section provides information about the hosting web server, including computer name, the version of ASP.NET you’re running, the domain name, and server time.
    • ASP.NET Server Variables. This section provides details about the many HTTP protocol details (called HTTP variables) and values that are part of each web page request.
    • HTTP Runtime Information. This section provides details about that the version of the Microsoft .NET Framework that your web page is running under, the path, details about the cache, and so on. (As you learned in Introduction to ASP.NET Web Programming Using the Razor Syntax, ASP.NET Web Pages using the Razor syntax are built on Microsoft’s ASP.NET web server technology, which is itself built on an extensive software development library called the .NET Framework.)
    • Environment Variables. This section provides a list of all the local environment variables and their values on the web server.

    A full description of all the server and request information is beyond the scope of this article, but you can see that the ServerInfo helper returns a lot of diagnostic information. For more information about the values that ServerInfo returns, see Recognized Environment Variables on the Microsoft TechNet website and IIS Server Variables on the MSDN website.

Embedding Output Expressions to Display Page Values

Another way to see what’s happening in your code is to embed output expressions in the page. As you know, you can directly output the value of a variable by adding something like @myVariable or @(subTotal * 12) to the page. For debugging, you can place these output expressions at strategic points in your code. This enables you to see the value of key variables or the result of calculations when your page runs. When you’re done debugging, you can remove the expressions or comment them out. This procedure illustrates a typical way to use embedded expressions to help debug a page.

  1. Create a new WebMatrix page that’s named OutputExpression.cshtml.
  2. Replace the page content with the following:

    [!code-htmlMain]

       1:  <!DOCTYPE html>
       2:  <html>
       3:      <head>
       4:          <title></title>
       5:      </head>
       6:      <body>   
       7:   
       8:      @{
       9:          var weekday = DateTime.Now.DayOfWeek;
      10:          // As a test, add 1 day to the current weekday.
      11:          if(weekday.ToString() != "Saturday") {
      12:              // If weekday is not Saturday, simply add one day.
      13:              weekday = weekday + 1; 
      14:          }
      15:          else {
      16:              // If weekday is Saturday, reset the day to 0, or Sunday.
      17:              weekday = 0; 
      18:          }
      19:          // Convert weekday to a string value for the switch statement.
      20:          var weekdayText = weekday.ToString(); 
      21:   
      22:          var greeting = "";
      23:          
      24:          switch(weekdayText) 
      25:          { 
      26:              case "Monday":
      27:                  greeting = "Ok, it's a marvelous Monday."; 
      28:                  break; 
      29:              case "Tuesday":
      30:                  greeting = "It's a tremendous Tuesday.";
      31:                  break; 
      32:              case "Wednesday":
      33:                  greeting = "Wild Wednesday is here!";
      34:                  break; 
      35:              case "Thursday":
      36:                  greeting = "All right, it's thrifty Thursday.";
      37:                  break;
      38:              case "Friday":
      39:                  greeting = "It's finally Friday!";
      40:                  break;
      41:              case "Saturday":
      42:                  greeting = "Another slow Saturday is here.";
      43:                  break;
      44:              case "Sunday":
      45:                  greeting = "The best day of all: serene Sunday.";
      46:                  break;
      47:              default:
      48:                  break; 
      49:          }
      50:      }
      51:      
      52:      <h2>@greeting</h2>
      53:   
      54:      </body>
      55:  </html>

    The example uses a switch statement to check the value of the weekday variable and then display a different output message depending on which day of the week it is. In the example, the if block within the first code block arbitrarily changes the day of the week by adding one day to the current weekday value. This is an error introduced for illustration purposes.
  3. Save the page and run it in a browser.

    The page displays the message for the wrong day of the week. Whatever day of the week it actually is, you’ll see the message for one day later. Although in this case you know why the message is off (because the code deliberately sets the incorrect day value), in reality it’s often hard to know where things are going wrong in the code. To debug, you need to find out what’s happening to the value of key objects and variables such as weekday.
  4. Add output expressions by inserting @weekday as shown in the two places indicated by comments in the code. These output expressions will display the values of the variable at that point in the code execution.

    [!code-csharpMain]
       1:  var weekday = DateTime.Now.DayOfWeek;
       2:  // DEBUG: Display the initial value of weekday. 
       3:  @weekday
       4:   
       5:  // As a test, add 1 day to the current weekday.
       6:  if(weekday.ToString() != "Saturday") {
       7:      // If weekday is not Saturday, simply add one day.
       8:      weekday = weekday + 1; 
       9:  }
      10:  else {
      11:      // If weekday is Saturday, reset the day to 0, or Sunday.
      12:      weekday = 0; 
      13:  }
      14:   
      15:  // DEBUG: Display the updated test value of weekday.
      16:  @weekday
      17:   
      18:  // Convert weekday to a string value for the switch statement.
      19:  var weekdayText = weekday.ToString();
  5. Save and run the page in a browser.

    The page displays the real day of the week first, then the updated day of the week that results from adding one day, and then the resulting message from the switch statement. The output from the two variable expressions (@weekday) has no spaces between the days because you didn’t add any HTML <p> tags to the output; the expressions are just for testing.

    Debugging-2
    Debugging-2
    Now you can see where the error is. When you first display the weekday variable in the code, it shows the correct day. When you display it the second time, after the if block in the code, the day is off by one. So you know that something has happened between the first and second appearance of the weekday variable. If this were a real bug, this kind of approach would help you narrow down the location of the code that’s causing the problem.
  6. Fix the code in the page by removing the two output expressions you added, and removing the code that changes the day of the week. The remaining, complete block of code looks like the following example:

    [!code-cshtmlMain]
       1:  @{
       2:      var weekday = DateTime.Now.DayOfWeek;
       3:      var weekdayText = weekday.ToString(); 
       4:   
       5:      var greeting = "";
       6:          
       7:      switch(weekdayText) 
       8:      { 
       9:          case "Monday":
      10:              greeting = "Ok, it's a marvelous Monday."; 
      11:              break; 
      12:          case "Tuesday":
      13:              greeting = "It's a tremendous Tuesday.";
      14:              break; 
      15:          case "Wednesday":
      16:              greeting = "Wild Wednesday is here!";
      17:              break; 
      18:          case "Thursday":
      19:              greeting = "All right, it's thrifty Thursday.";
      20:              break;
      21:          case "Friday":
      22:              greeting = "It's finally Friday!";
      23:              break;
      24:          case "Saturday":
      25:              greeting = "Another slow Saturday is here.";
      26:              break;
      27:          case "Sunday":
      28:              greeting = "The best day of all: serene Sunday.";
      29:              break;
      30:          default:
      31:              break; 
      32:      }
      33:  }
  7. Run the page in a browser. This time you see the correct message displayed for the actual day of the week.

Using the ObjectInfo Helper to Display Object Values

The ObjectInfo helper displays the type and the value of each object you pass to it. You can use it to view the value of variables and objects in your code (like you did with output expressions in the previous example), plus you can see data type information about the object.

  1. Open the file named OutputExpression.cshtml that you created earlier.
  2. Replace all code in the page with the following block of code:

    [!code-htmlMain]
       1:  <!DOCTYPE html>
       2:  <html>
       3:      <head>
       4:          <title></title>
       5:      </head>
       6:      <body>
       7:      @{
       8:        var weekday = DateTime.Now.DayOfWeek;
       9:        @ObjectInfo.Print(weekday)
      10:        var weekdayText = weekday.ToString(); 
      11:    
      12:        var greeting = "";
      13:    
      14:        switch(weekdayText) 
      15:        { 
      16:            case "Monday":
      17:                greeting = "Ok, it's a marvelous Monday."; 
      18:                break; 
      19:            case "Tuesday":
      20:                greeting = "It's a tremendous Tuesday.";
      21:                break; 
      22:            case "Wednesday":
      23:                greeting = "Wild Wednesday is here!";
      24:                break; 
      25:            case "Thursday":
      26:                greeting = "All right, it's thrifty Thursday.";
      27:                break;
      28:            case "Friday":
      29:                greeting = "It's finally Friday!";
      30:                break;
      31:             case "Saturday":
      32:                greeting = "Another slow Saturday is here.";
      33:                break;
      34:             case "Sunday":
      35:                greeting = "The best day of all: serene Sunday.";
      36:                break;
      37:            default:
      38:                break; 
      39:        }
      40:      }
      41:      @ObjectInfo.Print(greeting)
      42:      <h2>@greeting</h2>
      43:   
      44:      </body>
      45:  </html>
  3. Save and run the page in a browser.

    Debugging-4
    Debugging-4

    In this example, the ObjectInfo helper displays two items:

    • The type. For the first variable, the type is DayOfWeek. For the second variable, the type is String.
    • The value. In this case, because you already display the value of the greeting variable in the page, the value is displayed again when you pass the variable to ObjectInfo.

    For more complex objects, the ObjectInfo helper can display more information — basically, it can display the types and values of all of an object’s properties.

Using Debugging Tools in Visual Studio

For a more comprehensive debugging experience, use Visual Studio 2013 or the free Visual Studio Express 2013 for Web. With Visual Studio, you can set a breakpoint in your code at the line that you want to inspect.

set breakpoint
set breakpoint

When you test the web site, the executing code halts at the breakpoint.

reach breakpoint
reach breakpoint

You can examine the current values of the variables, and step through the code line-by-line.

see values
see values

For information about using the integrated debugger in Visual Studio to debug ASP.NET Razor pages, see Programming ASP.NET Web Pages (Razor) Using Visual Studio.

Additional Resources



Comments ( )
Link to this page: //www.vb-net.com/AspNet-DocAndSamples-2017/aspnet/web-pages/overview/testing-and-debugging/introduction-to-debugging.htm
< THANKS ME>