蓝色天空网mbsky.com · 知识 · 实践 · 创造Web Forms · 本地阅读预览
教程 / .NET / Web Forms

ASP.NET 简介

梳理经典 ASP 到 ASP.NET 的编译执行跃迁、Razor 标记定位、支持语言、服务器技术分支、历史与现代开发工具及四类核心文件扩展名。

本站独立整理的内容草稿。实例显示的是预期结果;浏览器、服务端运行与技术内容验收尚待完成,本页只用于阅读和图文核对。

学完这一章,你能够

从经典 ASP 到新一代 ASP.NET

经典 ASP(Active Server Pages,动态服务器页面)由微软于 1998 年发布,是其首个服务器端脚本引擎。它允许网页内嵌的脚本在 Web 服务器上解释执行,文件通常以 `.asp` 为扩展名并采用 VBScript 编写;读者如对历史动态页技术感兴趣,可结合经典的 ASP 基础章节加以对照。

ASP.NET 是架构全面重构的新一代框架,与经典 ASP 并不直接兼容(不过同一 IIS 站点内可并存运行经典 `.asp` 页面)。与解释执行的经典 ASP 不同,ASP.NET 页面会经过编译执行,运行性能显著提升,同时提供更强的多语言支持、丰富的用户控件、基于 XML 的配置组件与集成身份验证。传统 ASP.NET 页面扩展名为 `.aspx`,多使用 C# 或 VB.NET 编写(依托 .NET CLR,底层组件与控件也可使用多种托管语言编写)。当浏览器发起请求时,ASP.NET 引擎读取页面、按需编译并执行服务器端代码,最终只向浏览器返回标准的纯 HTML 响应。

ASP.NET Razor、编程语言与服务器技术

Razor 是微软设计的轻量级服务器端标记语法,可将服务器代码简洁地嵌入 HTML 网页中。它借鉴了经典 ASP 直观的页面书写方式,却具备完整 ASP.NET 编译与类型检查能力,在学习成本与编码效率之间取得了良好平衡。

本系列教程主要围绕 Visual Basic(VB.NET)与 C#(读作 C sharp)两种 .NET 语言展开,系统覆盖三类 ASP.NET 服务器端技术:基于 Razor 语法的 Web Pages、分层架构的 MVC(模型-视图-控制器)以及事件驱动的传统 Web Forms。进入 ASP.NET Core 时代后,Razor 与 C# 成为统一主力,VB.NET 不再支持 Razor 视图编译。

开发工具演进与核心文件扩展名

在历史教程环境中,ASP.NET 常见开发工具包括面向 Web Pages 的轻量工具 WebMatrix、面向 MVC 与 Web Forms 的免费精简版 Visual Web Developer 以及旗舰级集成开发环境 Visual Studio。需要注意的是,WebMatrix 与独立的 Visual Web Developer 均已退役,现代开发应改用 Visual Studio 2022(含 Community 免费版)或跨平台的 Visual Studio Code 搭配 C# Dev Kit 与 .NET SDK CLI。

识别不同年代的微软 Web 源文件可依据其文件扩展名:经典 ASP 页面使用 `.asp`;传统 ASP.NET Web Forms 页面使用 `.aspx`;采用 Razor C# 语法的页面使用 `.cshtml`;采用 Razor VB 语法的页面则使用 `.vbhtml`。

离线校验经典 ASP 与 ASP.NET 编译管道、四类扩展名路由及工具演进

python · 阅读源码
"""Offline ASP.NET semantic verification lab for Chapter 02 (aspnet)."""
import json

VERSION = "CPython 3.12.14 offline ASP.NET WebPages/Core semantic verifier (IIS/WebMatrix absent)"


def case_classic_asp_vs_aspnet_pipeline():
    classic_asp = {
        "released_year": 1998,
        "extension": ".asp",
        "default_script": "VBScript",
        "execution_mode": "interpreted",
        "compatible_with_aspnet": False,
    }
    aspnet = {
        "extension": ".aspx",
        "languages": ["C#", "VB.NET"],
        "execution_mode": "compiled",
        "response_format": "plain HTML",
        "integrated_auth": True,
    }
    pipeline_steps = ["browser_request", "engine_read", "compile_script", "execute_script", "return_html"]
    expected = {
        "classic_year": 1998,
        "classic_ext": ".asp",
        "aspnet_compiled": True,
        "pipeline_length": 5,
        "final_output": "return_html",
    }
    actual = {
        "classic_year": classic_asp["released_year"],
        "classic_ext": classic_asp["extension"],
        "aspnet_compiled": aspnet["execution_mode"] == "compiled",
        "pipeline_length": len(pipeline_steps),
        "final_output": pipeline_steps[-1],
    }
    return {
        "name": "classic_asp_vs_aspnet_pipeline",
        "summary": "对比经典 ASP(1998 解释执行 .asp)与 ASP.NET 编译执行管道及最终向浏览器返回纯 HTML 的机制",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_file_extension_routing_map():
    ext_map = {
        ".asp": ("Classic ASP", "VBScript"),
        ".aspx": ("ASP.NET Web Forms", "C# / VB.NET"),
        ".cshtml": ("ASP.NET Razor C#", "C#"),
        ".vbhtml": ("ASP.NET Razor VB", "VB.NET"),
    }
    test_files = ["legacy.asp", "Default.aspx", "Index.cshtml", "Home.vbhtml"]
    resolved = {f: ext_map["." + f.rsplit(".", 1)[1]][0] for f in test_files}
    expected = {
        "legacy.asp": "Classic ASP",
        "Default.aspx": "ASP.NET Web Forms",
        "Index.cshtml": "ASP.NET Razor C#",
        "Home.vbhtml": "ASP.NET Razor VB",
    }
    return {
        "name": "file_extension_routing_map",
        "summary": "校验 .asp、.aspx、.cshtml 与 .vbhtml 四类扩展名到对应服务器端引擎与编程语言的映射",
        "expected": expected,
        "actual": resolved,
        "passed": resolved == expected,
    }


def case_dev_tools_and_modern_migration():
    historical_tools = {
        "WebMatrix": "Web Pages",
        "Visual Web Developer": "MVC / Web Forms",
        "Visual Studio": "All ASP.NET workloads",
    }
    modern_tools = ["Visual Studio 2022", "Visual Studio Code + C# Dev Kit", ".NET SDK CLI"]
    expected = {
        "historical_count": 3,
        "webmatrix_target": "Web Pages",
        "vwd_target": "MVC / Web Forms",
        "modern_count": 3,
    }
    actual = {
        "historical_count": len(historical_tools),
        "webmatrix_target": historical_tools["WebMatrix"],
        "vwd_target": historical_tools["Visual Web Developer"],
        "modern_count": len(modern_tools),
    }
    return {
        "name": "dev_tools_and_modern_migration",
        "summary": "核对历史开发工具(WebMatrix、Visual Web Developer、Visual Studio)定位及现代跨平台 .NET 工具替代",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_razor_server_tech_trio():
    server_technologies = [
        ("Web Pages", "Razor"),
        ("MVC", "Model-View-Controller"),
        ("Web Forms", "Event-Driven"),
    ]
    expected = {"count": 3, "razor_backed": ["Web Pages", "MVC"]}
    actual = {
        "count": len(server_technologies),
        "razor_backed": ["Web Pages", "MVC"],
    }
    return {
        "name": "razor_server_tech_trio",
        "summary": "校验 ASP.NET 三大服务器端技术分支以及 Razor 标记语法在 Web Pages 与 MVC 中的共通角色",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = [
        case_classic_asp_vs_aspnet_pipeline(),
        case_file_extension_routing_map(),
        case_dev_tools_and_modern_migration(),
        case_razor_server_tech_trio(),
    ]
    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
预期结果 · 待实际运行核验
  • 离线验证器确认经典 ASP(1998/.asp) 与 ASP.NET 编译管道差异、四类扩展名精确映射、WebMatrix/VWD 到现代工具链迁移路径均校验通过。
离线校验经典 ASP 与 ASP.NET 编译管道、四类扩展名路由及工具演进

所需环境:Windows x64、CPython 3.12.14 离线 ASP.NET WebPages / Razor 语义验证器(本机未启动 IIS / IIS Express / WebMatrix 宿主环境)。

当前源码 SHA256:1bd689e6edeb888a1de4faff39541627a368c4254d8e94df578852b9e5c68521

下载本站示例源码

本地运行

解压后按 README 说明在对应本地环境运行脚本。

下载ASP.NET 简介实验

所需环境:Windows x64、CPython 3.12.14 离线 ASP.NET WebPages / Razor 语义验证器(本机未启动 IIS / IIS Express / WebMatrix 宿主环境)。

ZIP SHA256:638450d81b5f0e276e72cebc101437c2986aa79b8132c05089d02ee30c979335

常见错误与边界

  • 实际端口、文档资源与应用配置都应和测试条件一致。

练习与解答

为什么浏览器中“查看页面源代码”时无法看到 `.cshtml` 或 `.aspx` 文件里的 C# 服务器代码?

展开参考解答

因为 ASP.NET 引擎在服务器端读取、编译并执行页面中的服务器代码,最终发送给浏览器的响应体只有执行后生成的普通 HTML、CSS 与客户端 JavaScript。

规范与官方依据