ASP.NET/你的第一个页面
外观
< ASP.NET
没有一本编程书籍会没有“Hello World”示例作为第一个项目/示例。
在 ASP.NET 中,创建一个快速的“Hello World”示例是无痛且简单的。同时,我们将探索一个基本 ASP.NET 页面的结构。
让我们看看一个 ASP.NET 页面的基本结构。这样做将使我们有机会介绍与 ASP.NET 页面编码相关的几个重要方面。
VB.NET
<%@ Page Language="VB" %>
<html>
<head>
<title></title>
<script runat="server">
' ASP.NET page code goes here
Sub Page_Load()
'On Page Load Run this code
End Sub
</script>
</head>
<body>
<form runat="server">
<!-- ASP.NET controls go here -->
</form>
</body>
</html>
C#
<%@ Page Language="C#" debug="true" trace="false"%>
<html>
<head>
<title></title>
<script runat="server">
// ASP.NET page code goes here
void Page_Load() {
//On Page Load Run this Code
}
</script>
</head>
<body>
<form runat="server">
<!-- ASP.NET controls go here -->
</form>
</body>
</html>
如你所见,一个 ASP.NET 页面与一个普通的基于 HTML 的页面有很多共同点。现在,为了制作我们的“Hello World”示例,我们只需要在上面的示例中添加一行代码。
VB.Net
Sub Page_Load()
'On Page Load Run this code
Response.Write ("Hello World")
End Sub
C#
void Page_Load() {
//On Page Load Run this Code
Response.Write("Hello World");
}
将原始代码复制到一个扩展名为 aspx 的新文件中。然后进行上述修改。当文件运行时,假设它被托管在与 ASP.NET 兼容的 Web 服务器上,当你访问页面时,你将在你的 Web 浏览器上看到“Hello World”字样。