这篇文章将介绍几种不同的调试一个asp.net website 页面的方法。
你将学习到
- 如何显示帮助分析的信息和调试页面
- 如何在vs中使用调试工具
These are the ASP.NETfeatures introduced in the article
- The serverInfo helper
- objectInfo helper
ServerInfoHelper 是一个可以提供承载网页服务器的环境信息的调试工具,当一个浏览器发送请求的时候也会显示http的请求信息。serverInfo可以显示当前用户的身份,做出请求的浏览器类型等通常可以帮助解决问题的的信息
- 创建一个名为ServerInfo.cshtml的网页
- 在当前页面的结束时,在</body>标签前加入 @ServerInfo.GetHtml();
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
@ServerInfo.GetHtml()
</body>
</html>
你可以将其加入到页面的任何地方,但是在网页结尾加会更容易阅读
注意:你应该移除所有调试代码在你发布到实际的服务器上之前;对于其它的需要添加代码到页面的调试工具一样。否则暴露的服务器名,路径等对信息对网站安全不利
- 保存并运行
Server在页面中显示了一个表单信息
- 服务器配置信息Server Configuration ,这一段提供了服务器主机信息包括计算机名,asp.net版本,域名和服务器时间
- 服务器变量,这一段提供了许多http协议的细节和每一部分网页需求的值
- Http运行时信息,这一部分提供了网页运行的microsoft.net.framework的版本,路径,cache的详情等
- 环境变量,这一部分提供了本地环境变量和服务器环境变量
A full description of all the server and requestinformation is beyond the scope of this article, but you can see that the ServerInfo helper returns a lot ofdiagnostic information. For more information about the values thatServerInfo returns, see RecognizedEnvironment Variables on the Microsoft TechNet website and IIS Server Variables on the MSDNwebsite.
嵌入输出表达式来显示调试信息 另一种观测代码运行的方法就是将输出嵌入到网页。很直观的,我们可以通过加上一些东西例如@myvariable or@(subtotal*12)直接输出变量值到页面上。这使你在网页运行时可以看到一些关键的计算值。当你调试完了你可以移除这些代码或者将其注释,接下来的程序展示了使用一个典型的方法去嵌入表达来帮助调试
- 新建一个页面名为OutputExpression.cshtml
- 用接下来代码代替原有内容
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
@{
var weekday =DateTime.Now.DayOfWeek;
// As a test, add 1 day to thecurrent weekday.
if(weekday.ToString() !="Saturday") {
// If weekday is notSaturday, 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 stringvalue for the switch statement.
var weekdayText =weekday.ToString();
var greeting = "";
switch(weekdayText)
{
case"Monday":
greeting = "Ok, it'sa marvelous Monday.";
break;
case"Tuesday":
greeting = "It's atremendous Tuesday.";
break;
case"Wednesday":
greeting = "WildWednesday is here!";
break;
case"Thursday":
greeting = "Allright, it's thrifty Thursday.";
break;
case"Friday":
greeting = "It'sfinally Friday!";
break;
case"Saturday":
greeting = "Anotherslow Saturday is here.";
break;
case"Sunday":
greeting = "The bestday of all: serene Sunday.";
break;
default:
break;
}
}
<h2>@greeting</h2>
</body>
</html>
这个例子说明了使用一个switch语句去检查weekday变量的值,并且根据不同的值显示不同的输出信息。在例子中在第一块的if语句随意的修改weekday,是为了说明而刻意错误的地方
- 保存网页并运行
这个页面会显示一个错误的日子。事实上不管是星期几都会显示一个错误的日子。在这个例子中很容看出来为什么信息会错,但是事实上在调试中常常很难知道哪里出了错。为了调试,我们需要发现关键字像是weekday在代码运行时发生了什么
- 通过插入@weekday表达式 ,在代码运行时显示变量值
var weekday = DateTime.Now.DayOfWeek;
// DEBUG: 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;
}
// DEBUG: Display the updated test value of weekday.
@weekday
// Convert weekday to a string value for the switchstatement.
varweekdayText = weekday.ToString();
- 保存并运行,在页面中一开始显示了真实的日子,接下来被更新成了错误的。这样就很容易发现错误发生在哪。第一次显示正确第二次错误,说明第一次和第二次之间发生了什么。这种方法可以缩小错误发生的范围
- 修改代码,去除中间的两次输出,去除bug。剩下的代码就应该是这个样子
@{
var weekday =DateTime.Now.DayOfWeek;
var weekdayText = weekday.ToString();
var greeting = "";
switch(weekdayText)
{
case"Monday":
greeting = "Ok, it's amarvelous Monday.";
break;
case "Tuesday":
greeting = "It's atremendous Tuesday.";
break;
case "Wednesday":
greeting = "WildWednesday is here!";
break;
case "Thursday":
greeting = "All right,it's thrifty Thursday.";
break;
case "Friday":
greeting = "It's finallyFriday!";
break;
case "Saturday":
greeting = "Another slowSaturday is here.";
break;
case "Sunday":
greeting = "The best dayof all: serene Sunday.";
break;
default:
break;
}
}
- 运行,这次应该能返回正确的星期几了
使用objectInfo helper 来显示对象值,ObjectInfo helper显示每一个传递的对象的类型和值。我们可以用它在代码里去观测变量和对象
- 打开一开始创房间的OutputExpression.cshtml
- 用下面的代码替换原有代码
<!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 amarvelous Monday.";
break;
case"Tuesday":
greeting = "It's atremendous Tuesday.";
break;
case"Wednesday":
greeting = "WildWednesday is here!";
break;
case"Thursday":
greeting = "All right,it's thrifty Thursday.";
break;
case"Friday":
greeting = "It'sfinally Friday!";
break;
case"Saturday":
greeting = "Anotherslow Saturday is here.";
break;
case"Sunday":
greeting = "The bestday of all: serene Sunday.";
break;
default:
break;
}
}
@ObjectInfo.Print(greeting)
<h2>@greeting</h2>
</body>
</html>
- 保存并运行
例子中,ObjectInfo helper 显示了两项
- 类型,第一种变量类型是DayOfWeek,第二个变量值是String
- 值
更复杂的对象,ObjectInfo helper可以显示更多信息
使用vs的调试工具
- 打断点
- 运行
- 检查值