Advanced Testing and Debugging of an ASP.NET 2.0 Application - How to write information directly to the HTTP output stream
(Page 4 of 4 )
Another way to display information as a program executes is to write it directly to the HTTP output stream. To do that, you use the Write method of the HTTP Response object as shown in figure 4-17. When you use this technique, you’ll want to be sure to remove any statements you’ve added when you finish testing your application.
At the top of this figure, you can see a Cart page that includes output that indicates the number of items that are currently in the shopping cart. To generate this output, I added a Response.Write method to the Page_Load event handler of the page. As you can see, this event handler uses the Count property of the Cart object to determine the number of items that are in the cart.
Notice that the text you include on the Write method can include HTML tags. For example, the Write method shown here includes a <br /> tag so that the item count is followed by a blank line. Also note that the output you write to the output stream is always added to the beginning of the stream. Because of that, it always appears at the top of the browser window.
The Cart page with output generated by Response.Write

Figure 4-17. How to write information directly to the HTTP output stream
A Page_Load event handler that writes to the HTTP output stream
protected void Page_Load(object sender, EventArgs e)
{
this.GetCart();
if (!IsPostBack)
{
this.DisplayCart();
Response.Write("Items in cart = " + cart.Count + "<br />");
}
}
Code that writes HTML output from another class
HttpContext.Current.Response.Write("Now updating file.<br />");
Description
- The Write method of the HttpResponse object provides a convenient way to write data directly to the HTTP output stream. The output can include any valid HTML.
- To access the HttpResponse object from the code-behind file for a web page, you use the Response property of the page. To access this object from a class that doesn’t inherit the Page class, you use the Response property of the HttpContext object for the current request. To access this object, you use the Current property of the HttpContext class.
- The HTML output you add to the HTTP output stream using the Write method of the HttpResponse object appears at the beginning of the output stream. As a result, the output from the Write method always appears at the top of the page in the browser window.
Perspective
As you can now appreciate, Visual Studio provides a powerful set of tools for debugging ASP.NET applications. For simple applications, you can usually get the debugging done just by using breakpoints, data tips, and the Autos window. You may also need to step through critical portions of code from time to time. For complex applications, though, you may discover the need for some of the other features that are presented in this chapter. With tools like these, a difficult debugging job becomes manageable.
Terms
virtual directory
virtual root
active mode
passive mode
Remote Debug Monitor
browse location
concurrency
concurrency error
break mode
Exception Assistant
stack trace
debugger
breakpoint
Breakpoints window
tracepoint
Output window
data tip
Immediate window
Autos window
Locals window
Watch window
watch expression
Trace feature
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter four of the book Murach's ASP.NET 2.0 Web Programming with VB 2005, written by Joel Murach and Anne Boehm (Murach, 2006; ISBN: 1-890774-32-4). Check it out today at your favorite bookstore. Buy this book now.
|
|