蓝色天空.知识 · 实践 · 创造
我的收藏
MVC 教程目录 55章
MVC / LEARN BY DOING

WebPages 简介

介绍 ASP.NET Web Pages 单页面模式的核心优势、首段 @DateTime.Now 动态输出示例、参考体系以及历史工具 WebMatrix 与现代 ASP.NET Core Razor Pages 迁移指引。

学完这一章,你能够

  • 介绍 ASP.NET Web Pages 单页面模式的核心优势、首段 @DateTime.Now 动态输出示例、参考体系以及历史工具 WebMatrix 与现代 ASP.NET Core Razor Pages 迁移指引。

Web Pages 在 ASP.NET 中的定位与首例体验

ASP.NET 结合 HTML、CSS、JavaScript 与服务器端脚本构建网站,并提供 Web Pages、MVC 与 Web Forms 三种传统开发模式。从本章起,我们将系统进入 Web Pages 模块的学习。

学习一门 Web 新技术最高效的方式,往往是从直观的最小可运行实例切入。在页面中写入 `<h1>Hello Web Pages</h1>` 与 `<p>The time is @DateTime.Now</p>` 后,服务器在响应请求时会自动计算 `@DateTime.Now` 并输出为当前日期时间文本,同时对动态插值执行默认的 HTML 编码转义。通过对比服务端模板源码与最终 HTML 响应,初学者能够迅速建立服务器端渲染的直觉。

什么是 Web Pages 及其学习资源

Web Pages 是三种传统 ASP.NET 编程模式(另两种为 Web Forms 与 MVC)中最容易上手的一种。它将前端标记与后端逻辑融于单一页面,具备五项突出特征:易于理解和使用、围绕单网页组织、编程体验贴近 PHP 与经典 ASP、原生支持 C# 与 VB 服务器脚本、并保留对 HTML、CSS 与 JavaScript 的完全控制。同时,Web Pages 内建了面向数据库、视频、图形与社交集成等场景的 Web Helpers,便于以极少代码扩展功能。

如果你刚开始学习 ASP.NET,建议先通过 Web Pages 掌握 Razor 服务器标记与 Web Helpers 的用法。由于服务端代码只在服务器执行,浏览器端只能看到渲染后的普通 HTML;因此本站所有实例都会同时呈现服务器端源码与对应的渲染输出,便于对照理解。教程后续还将提供完整的 Web Pages 实例汇总与对象/组件/属性/方法参考手册,方便日常检索。

WebMatrix 历史工具与现代 ASP.NET Core 迁移

早期 Web Pages 教程配套使用了微软免费工具 WebMatrix。它是一套面向初学者的一体化轻量套件,集成五部分能力:Web Pages 实例与站点模板、C#/VB 的 Razor 服务器标记引擎、轻量 Web 服务器 IIS Express、嵌入式数据库 SQL Server Compact,以及完整的 ASP.NET 框架。

借助 WebMatrix,开发者既能从空白站点起步,也能从“Web 应用程序库”安装 Umbraco、DotNetNuke、Drupal、Joomla、WordPress 等开源 ASP.NET 或 PHP 应用进行二次开发,并利用其内置的安全、SEO 与发布工具;早年官方安装入口曾位于 `http://www.microsoft.com/web/gallery/install.aspx?appid=WebMatrix`。需重点提示:微软已于 2017 年停止支持 WebMatrix,SQL Server Compact 4.0 亦已停止维护;现代 Web 开发请直接安装跨平台 .NET SDK 并使用 ASP.NET Core Razor Pages 搭配 Visual Studio 2022 或 VS Code。

ASP.NET 三种开发模式导航对照

当前主题分层框架对照传统窗体对照
Web Pages(本模块主讲:单页面 Razor 模式,现代演进为 ASP.NET Core Razor Pages)MVC(Model-View-Controller 分层模式,现代演进为 ASP.NET Core MVC)Web Forms(传统事件驱动服务器控件模式,仅限 .NET Framework)

离线校验 Web Pages @DateTime.Now 渲染、默认 HTML 转义与 WebMatrix 组件迁移

含完整源码 · 123 行
展开查看收起
python
"""Offline ASP.NET semantic verification lab for Chapter 03 (webpages-intro)."""
import html
import json
import re

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


def render_inline_razor(template: str, context: dict) -> str:
    def repl(match):
        expr = match.group(1)
        val = context[expr]
        return html.escape(str(val), quote=True)
    return re.sub(r"@([A-Za-z_][A-Za-z0-9_.]*)", repl, template)


def case_hello_webpages_datetime_render():
    template = (
        "<html>\n"
        "<body>\n"
        "     <h1>Hello Web Pages</h1>\n"
        "     <p>The time is @DateTime.Now</p>\n"
        "</body>\n"
        "</html>"
    )
    rendered = render_inline_razor(template, {"DateTime.Now": "2026/12/01 09:30:00"})
    expected = {
        "has_server_token_in_output": False,
        "rendered_paragraph": "<p>The time is 2026/12/01 09:30:00</p>",
        "browser_sees_only_html": True,
    }
    actual = {
        "has_server_token_in_output": "@DateTime.Now" in rendered,
        "rendered_paragraph": rendered.splitlines()[3].strip(),
        "browser_sees_only_html": "<h1>Hello Web Pages</h1>" in rendered and "@" not in rendered,
    }
    return {
        "name": "hello_webpages_datetime_render",
        "summary": "模拟 Web Pages 服务器端求值 @DateTime.Now 并向浏览器返回纯 HTML 输出(隐藏服务端源码)",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_webpages_vs_mvc_webforms_strip():
    modes = ("Web Pages", "MVC", "Web Forms")
    traits = [
        "easy_to_learn",
        "single_page_centric",
        "similar_to_php_and_classic_asp",
        "vb_or_csharp_server_script",
        "full_html_css_js_control",
    ]
    expected = {"modes": ["Web Pages", "MVC", "Web Forms"], "trait_count": 5}
    actual = {"modes": list(modes), "trait_count": len(traits)}
    return {
        "name": "webpages_vs_mvc_webforms_strip",
        "summary": "校验 ASP.NET 三模式横栏条目与 Web Pages 单页面开发模式的五项核心特征",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_webmatrix_bundle_components():
    bundle = {
        "templates": "Web Pages templates & examples",
        "language": "Razor (C# / VB)",
        "web_server": "IIS Express",
        "database": "SQL Server Compact",
        "framework": "ASP.NET",
    }
    open_source_gallery = ["Umbraco", "DotNetNuke", "Drupal", "Joomla", "WordPress"]
    expected = {
        "component_count": 5,
        "web_server": "IIS Express",
        "database": "SQL Server Compact",
        "gallery_apps": 5,
        "eol_migrated_to": "ASP.NET Core Razor Pages + .NET SDK",
    }
    actual = {
        "component_count": len(bundle),
        "web_server": bundle["web_server"],
        "database": bundle["database"],
        "gallery_apps": len(open_source_gallery),
        "eol_migrated_to": "ASP.NET Core Razor Pages + .NET SDK",
    }
    return {
        "name": "webmatrix_bundle_components",
        "summary": "核对历史 WebMatrix 套件五大组件、开源应用库代表项目及现代 ASP.NET Core Razor Pages 迁移目标",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_razor_html_auto_encoding():
    template = "<p>Note: @userInput</p>"
    rendered = render_inline_razor(template, {"userInput": "<script>alert('x')</script>"})
    expected = "<p>Note: &lt;script&gt;alert(&#x27;x&#x27;)&lt;/script&gt;</p>"
    return {
        "name": "razor_html_auto_encoding",
        "summary": "校验 Razor 内联 @ 表达式默认执行 HTML 实体转义以防止脚本注入",
        "expected": expected,
        "actual": rendered,
        "passed": rendered == expected,
    }


def main():
    cases = [
        case_hello_webpages_datetime_render(),
        case_webpages_vs_mvc_webforms_strip(),
        case_webmatrix_bundle_components(),
        case_razor_html_auto_encoding(),
    ]
    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
离线验证器确认 @DateTime.Now 在服务端求值并输出纯 HTML、特殊字符自动 HTML 编码防 XSS、三大模式横栏与 WebMatrix 五组件迁移核对全部通过。

查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包

本地运行

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

下载本章实验包 (mbskycom202612a2870.zip) ↓ 查看运行说明 ↗

本章附件与实例包

下载课程关联工程包:MVC

课程关联工程包(登记关联55章) · 含当前章节或实例 · mbskycom202612a2870.zip

下载课程关联工程包:MVC

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2897.zip

常见错误与边界检查

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

技术规范与官方资料

练习与参考解答

查看参考解答

因为 WebMatrix 与 SQL Server Compact 均已终止官方支持且仅限 Windows;ASP.NET Core Razor Pages 继承了以页面为中心的简洁模型,同时具备跨平台、高性能、依赖注入与长期安全更新支持。

SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习