"""Offline ASP.NET WebForms verifier for Chapter 37 (intro)."""
from __future__ import annotations
import html
import json
import re
VERSION = "CPython 3.12.14 offline ASP.NET WebForms verifier (IIS/ASP.NET runtime absent)"
def compile_webforms_snippet(aspx: str, server_state: dict[str, str]) -> str:
"""Simulate server-side compilation of a minimal WebForms page with server controls."""
def replace_label(match: re.Match[str]) -> str:
ctrl_id = match.group(1)
text = server_state.get(ctrl_id, "")
return f'<span id="{html.escape(ctrl_id, quote=True)}">{html.escape(text)}</span>'
rendered = re.sub(
r'<asp:Label\s+id="([^"]+)"\s+runat="server"\s*/>',
replace_label,
aspx,
flags=re.IGNORECASE,
)
rendered = re.sub(
r'<form\s+runat="server">',
'<form method="post" action="default.aspx" id="form1">',
rendered,
flags=re.IGNORECASE,
)
return rendered
def main() -> None:
models = {
"Web Pages": {"style": "inline_razor_script", "focus": False},
"MVC": {"style": "model_view_controller", "focus": False},
"Web Forms": {"style": "event_driven_server_controls", "focus": True},
}
focused = [name for name, meta in models.items() if meta["focus"]]
aspx_src = (
'<html><body><form runat="server">'
'<asp:Label id="lblGreeting" runat="server" />'
"</form></body></html>"
)
rendered_html = compile_webforms_snippet(
aspx_src,
{"lblGreeting": "Welcome to WebForms <Server Controls>"},
)
expected_html = (
'<html><body><form method="post" action="default.aspx" id="form1">'
'<span id="lblGreeting">Welcome to WebForms <Server Controls></span>'
"</form></body></html>"
)
vs_express_components = [
"MVC 和 Web Forms",
"拖拽 Web 控件和 Web 组件",
"Web 服务器语言(VB 或 C#)",
"Web 服务器(IIS Express)",
"数据库服务器(SQL Server Compact)",
"完整的 Web 开发框架(ASP.NET)",
]
modern_mapping = {
"Visual Studio Express 2012/2010": "Visual Studio Community / VS Code + C# Dev Kit",
"Web Forms (.NET Framework)": "ASP.NET Core Razor Pages / Blazor",
"IIS Express": "Kestrel + 反向代理(IIS/Nginx)",
"SQL Server Compact": "SQLite / SQL Server LocalDB",
}
reference_topics = ["对象", "组件", "属性", "方法"]
cases = [
{
"name": "aspnet_three_models_matrix",
"summary": "核对 ASP.NET 传统三种编程模式(Web Pages、MVC、Web Forms)定位及本专题聚焦",
"expected": {"count": 3, "names": ["Web Pages", "MVC", "Web Forms"], "focused": ["Web Forms"]},
"actual": {"count": len(models), "names": list(models.keys()), "focused": focused},
"passed": len(models) == 3 and focused == ["Web Forms"],
},
{
"name": "webforms_server_compile_pipeline",
"summary": "模拟 WebForms 服务器端编译并生成纯 HTML,确认无 <asp:> 或 runat 残留且完成 HTML 转义",
"expected": {
"html": expected_html,
"has_server_tag": False,
"has_runat": False,
},
"actual": {
"html": rendered_html,
"has_server_tag": "<asp:" in rendered_html.lower(),
"has_runat": 'runat="server"' in rendered_html.lower(),
},
"passed": (
rendered_html == expected_html
and "<asp:" not in rendered_html.lower()
and 'runat="server"' not in rendered_html.lower()
),
},
{
"name": "vs_express_components_and_modern_mapping",
"summary": "校验 Visual Studio Express 历史六大组件清单及现代 ASP.NET Core / Blazor 演进映射",
"expected": {
"legacy_component_count": 6,
"modern_web_successor": "ASP.NET Core Razor Pages / Blazor",
"modern_db_successor": "SQLite / SQL Server LocalDB",
},
"actual": {
"legacy_component_count": len(vs_express_components),
"modern_web_successor": modern_mapping["Web Forms (.NET Framework)"],
"modern_db_successor": modern_mapping["SQL Server Compact"],
},
"passed": (
len(vs_express_components) == 6
and modern_mapping["Web Forms (.NET Framework)"] == "ASP.NET Core Razor Pages / Blazor"
and modern_mapping["SQL Server Compact"] == "SQLite / SQL Server LocalDB"
),
},
{
"name": "aspnet_reference_scope_check",
"summary": "核对 ASP.NET 参考手册涵盖对象、组件、属性与方法四大核心维度",
"expected": ["对象", "组件", "属性", "方法"],
"actual": reference_topics,
"passed": reference_topics == ["对象", "组件", "属性", "方法"],
},
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 离线 ASP.NET WebForms 语义验证器(本机未启动 IIS / ASP.NET WebForms 运行时宿主)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 离线 ASP.NET WebForms 语义验证器(本机未启动 IIS / ASP.NET WebForms 运行时宿主)。
入口文件:projects/mvc-aspnet-intro-lab/explore-aspnet-intro.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-aspnet-intro.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 ASP.NET WebForms 语义验证器(本机未启动 IIS / ASP.NET WebForms 运行时宿主)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2860.zip
课程关联工程包(登记关联55章) · 含当前章节或实例 · mbskycom202612a2870.zip