41
ASP.NET Web Pages Using The Razor Syntax
Chapter 17 – Introduction to Debugging
189
Note 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 chapter 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. (Make sure the page is selected in the Files workspace
before you run it.)
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 Chapter 2 - 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 chapter,
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.
57
ASP.NET Web Pages Using The Razor Syntax
Chapter 17 – Introduction to Debugging
190
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:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
@{
var weekday = DateTime.Now.DayOfWeek;
// As a test, add 1 day to the current weekday.
if(weekday.ToString() != "Saturday") {
// If weekday is not Saturday, simply add one day.
weekday = weekday + 1;
}
else {
// If weekday is Saturday, reset the day to 0, or Sunday.
weekday = 0;
}
// Convert weekday to a string value for the switch statement.
var weekdayText = weekday.ToString();
var greeting = "";
switch(weekdayText)
{
case "Monday":
greeting = "Ok, it's a marvelous Monday.";
break;
case "Tuesday":
greeting = "It's a tremendous Tuesday.";
break;
case "Wednesday":
greeting = "Wild Wednesday is here!";
break;
case "Thursday":
greeting = "All right, it's thrifty Thursday.";
break;
case "Friday":
greeting = "It's finally Friday!";
break;
case "Saturday":
greeting = "Another slow Saturday is here.";
break;
55
ASP.NET Web Pages Using The Razor Syntax
Chapter 17 – Introduction to Debugging
191
case "Sunday":
greeting = "The best day of all: serene Sunday.";
break;
default:
break;
}
}
<h2>@greeting</h2>
</body>
</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.
var weekday = DateTime.Now.DayOfWeek;
// Display the initial value of weekday.
@weekday
// As a test, add 1 day to the current weekday.
if(weekday.ToString() != "Saturday") {
// If weekday is not Saturday, simply add one day.
weekday = weekday + 1;
}
else {
// If weekday is Saturday, reset the day to 0, or Sunday.
weekday = 0;
}
// Display the updated test value of weekday.
@weekday
// Convert weekday to a string value for the switch statement.
var weekdayText = weekday.ToString();
5.
Save and run the page in a browser.
C# Word - Convert Word to HTML in C#.NET VB.NET How-to, VB.NET PDF, VB.NET Word, VB toolkit SDK, preserves all the original anchors, links, bookmarks and font C#: Convert Word document to HTML5 files.
bookmark page in pdf; export pdf bookmarks to excel
49
ASP.NET Web Pages Using The Razor Syntax
Chapter 17 – Introduction to Debugging
192
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
) have no spaces between them because you didn't
add any HTML
<p>
tags to the output; the expressions are just for testing.
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:
@{
var weekday = DateTime.Now.DayOfWeek;
var weekdayText = weekday.ToString();
var greeting = "";
switch(weekdayText)
{
case "Monday":
greeting = "Ok, it's a marvelous Monday.";
break;
case "Tuesday":
greeting = "It's a tremendous Tuesday.";
break;
case "Wednesday":
greeting = "Wild Wednesday is here!";
break;
case "Thursday":
greeting = "All right, it's thrifty Thursday.";
break;
case "Friday":
greeting = "It's finally Friday!";
break;
case "Saturday":
greeting = "Another slow Saturday is here.";
42
ASP.NET Web Pages Using The Razor Syntax
Chapter 17 – Introduction to Debugging
193
break;
case "Sunday":
greeting = "The best day of all: serene Sunday.";
break;
default:
break;
}
}
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:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
@{
var weekday = DateTime.Now.DayOfWeek;
@ObjectInfo.Print(weekday)
var weekdayText = weekday.ToString();
var greeting = "";
switch(weekdayText)
{
case "Monday":
greeting = "Ok, it's a marvelous Monday.";
break;
case "Tuesday":
greeting = "It's a tremendous Tuesday.";
47
ASP.NET Web Pages Using The Razor Syntax
Chapter 17 – Introduction to Debugging
194
break;
case "Wednesday":
greeting = "Wild Wednesday is here!";
break;
case "Thursday":
greeting = "All right, it's thrifty Thursday.";
break;
case "Friday":
greeting = "It's finally Friday!";
break;
case "Saturday":
greeting = "Another slow Saturday is here.";
break;
case "Sunday":
greeting = "The best day of all: serene Sunday.";
break;
default:
break;
}
}
@ObjectInfo.Print(greeting)
<h2>@greeting</h2>
</body>
</html>
3.
Save and run the page in a browser.
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.
36
ASP.NET Web Pages Using The Razor Syntax
Chapter 17 – Introduction to Debugging
195
Using Debugging Tools
In addition to displaying information in the page to help you debug, you can use tools that provide
information about how your pages are running. This section shows you how to use the most popular
diagnostic tools for web pages, and how to use some tools in WebMatrix that also can help you debug
your site.
Internet Explorer Developer Tools
Internet Explorer Developer Tools is a package of web tools built into Internet Explorer 8. (For previous
versions of Internet Explorer, you can install the tools from the Internet Explorer Developer Toolbar
page on the Microsoft Download Center.) This tool does not specifically let you debug ASP.NET code, but
can be very useful for debugging HTML, CSS, and script, including the markup and script that's generated
dynamically by ASP.NET.
This procedure gives you an idea of how to work with the Internet Explorer Developer Tools. It assumes
you're working with Internet Explorer 8.
1.
In Internet Explorer, browse to a public web page such as www.microsoft.com.
2.
In the Tools menu, click Developer Tools.
3.
Click the HTML tab, open the
<html>
element, and then open the
<body>
element. The window
shows you all the tags in the
<body>
element.
4.
In the right-hand pane, click the Attributes tab to see the attributes for the
<body>
tag:
5.
In the right-hand pane, click Style to see the CSS styles that apply to the body section of the
page.
31
ASP.NET Web Pages Using The Razor Syntax
Chapter 17 – Introduction to Debugging
196
To learn more the Internet Explorer Developer Tools, see Discovering the Internet Explorer
Developer Tools on the MSDN website.
Firebug
Firebug is an add-on for Mozilla Firefox that lets you inspect HTML markup and CSS, debug client script,
and view cookies and other page information. You can install Firebug from the Firebug website
(http://getfirebug.com/). As with the Internet Explorer debugging tools, this tool does not specifically let
you debug ASP.NET code, but can be very useful for examining the HTML and other page elements,
including those that ASP.NET generates dynamically.
This procedure shows you a few of the things you can do with Firebug after you've installed it.
1.
In Firebox, browse to www.microsoft.com.
2.
In the Tools menu, click Firebug, and then click Open Firebug in New Window.
3.
In the Firebug main window, click the HTML tab and then expand the
<html>
node in the left
pane.
4.
Select the
<body>
tag, and then click the Style tab in the right pane. Firebug displays style
information about the Microsoft site.
Firebug includes many options for editing and validating your HTML and CSS styles, and for
debugging and improving your script. In the Net tab, you can analyze the network traffic
between a server and a web page. For example, you can profile your page and see how long it
takes to download all the content to a browser. To learn more about Firebug, see the Firebug
main site and the Firebug Documentation Wiki.
Documents you may be interested
Documents you may be interested