"""Offline ASP.NET semantic verification lab for Chapter 05 (webpages-layout)."""
import json
import re
VERSION = "CPython 3.12.14 offline ASP.NET WebPages/Core semantic verifier (IIS/WebMatrix absent)"
def case_render_page_content_blocks():
partials = {
"header.cshtml": "<header>Site Header</header>",
"footer.cshtml": "<footer>Site Footer</footer>",
}
page = (
'<html><body>@RenderPage("header.cshtml")'
"<h1>Hello Web Pages</h1><p>This is a paragraph</p>"
'@RenderPage("footer.cshtml")</body></html>'
)
rendered = re.sub(r'@RenderPage\("([^"]+)"\)', lambda m: partials[m.group(1)], page)
expected = (
"<html><body><header>Site Header</header>"
"<h1>Hello Web Pages</h1><p>This is a paragraph</p>"
"<footer>Site Footer</footer></body></html>"
)
return {
"name": "render_page_content_blocks",
"summary": "模拟 @RenderPage() 将独立 header.cshtml 与 footer.cshtml 内容块嵌入页面指定位置",
"expected": expected,
"actual": rendered,
"passed": rendered == expected,
}
def case_layout_page_render_body():
layouts = {
"_Layout.cshtml": (
"<html><body><p>This is header text</p>"
"@RenderBody()"
"<p>© 2026 Example Site. All rights reserved.</p></body></html>"
),
}
content_page = (
'@{Layout="_Layout.cshtml";}'
"<h1>Welcome to Example Site</h1>"
"<p>Consistent layout body content.</p>"
)
m = re.match(r'^@\{Layout="([^"]+)";\}(.*)$', content_page, re.S)
layout_name, body_html = m.group(1), m.group(2)
rendered = layouts[layout_name].replace("@RenderBody()", body_html)
expected = (
"<html><body><p>This is header text</p>"
"<h1>Welcome to Example Site</h1><p>Consistent layout body content.</p>"
"<p>© 2026 Example Site. All rights reserved.</p></body></html>"
)
return {
"name": "layout_page_render_body",
"summary": "模拟内容页通过 @{Layout=\"_Layout.cshtml\";} 绑定布局页并由 @RenderBody() 替换正文区域",
"expected": expected,
"actual": rendered,
"passed": rendered == expected,
}
def case_underscore_prefix_browse_protection():
requests = [
"/_header.cshtml",
"/_footer.cshtml",
"/_Layout.cshtml",
"/_AppStart.cshtml",
"/Default.cshtml",
"/Shared/_Layout.cshtml",
]
def can_browse(path: str) -> bool:
filename = path.rsplit("/", 1)[-1]
return not filename.startswith("_")
actual = {p: (200 if can_browse(p) else 404) for p in requests}
expected = {
"/_header.cshtml": 404,
"/_footer.cshtml": 404,
"/_Layout.cshtml": 404,
"/_AppStart.cshtml": 404,
"/Default.cshtml": 200,
"/Shared/_Layout.cshtml": 404,
}
return {
"name": "underscore_prefix_browse_protection",
"summary": "验证 ASP.NET 禁止浏览器直接访问下划线开头文件(_Layout/_header/_footer/_AppStart)的规则",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_appstart_webmail_config_and_secret_hygiene():
appstart_cfg = {
"SmtpServer": "mailserver.example.com",
"EnableSsl": True,
"UserName": "username@example.com",
"Password": "<env:WEBMAIL_PASSWORD>",
"From": "your-name-here@example.com",
}
expected = {
"ssl_enabled": True,
"required_keys": 5,
"direct_http_blocked": True,
"plaintext_secret_avoided": True,
}
actual = {
"ssl_enabled": appstart_cfg["EnableSsl"] is True,
"required_keys": len(appstart_cfg),
"direct_http_blocked": "_AppStart.cshtml".startswith("_"),
"plaintext_secret_avoided": appstart_cfg["Password"].startswith("<env:"),
}
return {
"name": "appstart_webmail_config_and_secret_hygiene",
"summary": "核对 _AppStart.cshtml 集中配置 WebMail 参数结构,并验证现代工程使用环境变量/机密管理替代硬编码口令",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_render_page_content_blocks(),
case_layout_page_render_body(),
case_underscore_prefix_browse_protection(),
case_appstart_webmail_config_and_secret_hygiene(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 离线 ASP.NET WebPages / Razor 语义验证器(本机未启动 IIS / IIS Express / WebMatrix 宿主环境)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 离线 ASP.NET WebPages / Razor 语义验证器(本机未启动 IIS / IIS Express / WebMatrix 宿主环境)。
入口文件:projects/web-forms-webpages-layout-lab/explore-webpages-layout.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-webpages-layout.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 ASP.NET WebPages / Razor 语义验证器(本机未启动 IIS / IIS Express / WebMatrix 宿主环境)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联55章) · 含当前章节或实例 · mbskycom202612a2981.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a3010.zip