"""Offline ASP.NET semantic verification lab for Chapter 06 (webpages-folders)."""
import json
from pathlib import PureWindowsPath
VERSION = "CPython 3.12.14 offline ASP.NET WebPages/Core semantic verifier (IIS/WebMatrix absent)"
def map_path(app_root: str, virtual_path: str) -> str:
root = PureWindowsPath(app_root)
rel = virtual_path
if rel.startswith("~/"):
rel = rel[2:]
elif rel.startswith("/"):
rel = rel[1:]
parts = [p for p in rel.replace("\\", "/").split("/") if p and p != "."]
if ".." in parts:
raise ValueError("Path traversal outside application root is forbidden")
return str(root.joinpath(*parts))
def href_resolve(app_base: str, virtual_path: str) -> str:
base = app_base.rstrip("/")
if virtual_path.startswith("~/"):
return f"{base}/{virtual_path[2:]}"
if virtual_path.startswith("/"):
return virtual_path
return f"{base}/{virtual_path}"
def case_tilde_virtual_paths():
my_images_folder = "~/images"
my_stylesheet = "~/styles/StyleSheet.css"
root_a = r"C:\Documents\MyWebSites\Demo"
root_b = r"D:\Deploy\PortalV2"
expected = {
"root_a_images": r"C:\Documents\MyWebSites\Demo\images",
"root_b_css": r"D:\Deploy\PortalV2\styles\StyleSheet.css",
"code_unchanged_on_move": True,
}
actual = {
"root_a_images": map_path(root_a, my_images_folder),
"root_b_css": map_path(root_b, my_stylesheet),
"code_unchanged_on_move": my_images_folder.startswith("~/") and my_stylesheet.startswith("~/"),
}
return {
"name": "tilde_virtual_paths",
"summary": "验证 ~ 运算符定义应用根相对虚拟路径,在站点物理目录迁移后无需改动源码",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_server_mappath_and_traversal_guard():
app_root = r"C:\Documents\MyWebSites\Demo"
mapped_data = map_path(app_root, "~/dataFile.txt")
mapped_appdata = map_path(app_root, "~/App_Data/Persons.txt")
blocked = False
try:
map_path(app_root, "~/../secret.config")
except ValueError:
blocked = True
expected = {
"data_file": r"C:\Documents\MyWebSites\Demo\dataFile.txt",
"app_data_file": r"C:\Documents\MyWebSites\Demo\App_Data\Persons.txt",
"traversal_blocked": True,
}
actual = {
"data_file": mapped_data,
"app_data_file": mapped_appdata,
"traversal_blocked": blocked,
}
return {
"name": "server_mappath_and_traversal_guard",
"summary": "模拟 Server.MapPath 将虚拟路径解析为服务器物理路径,并拦截越界路径遍历",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_href_browser_url_conversion():
css_virtual = "~/Shared/Site.css"
root_hosted = href_resolve("", css_virtual)
subapp_hosted = href_resolve("/portal", css_virtual)
link_tag = f'<link rel="stylesheet" type="text/css" href="{root_hosted}" />'
expected = {
"root_hosted": "/Shared/Site.css",
"subapp_hosted": "/portal/Shared/Site.css",
"link_tag": '<link rel="stylesheet" type="text/css" href="/Shared/Site.css" />',
"no_tilde_in_html": True,
}
actual = {
"root_hosted": root_hosted,
"subapp_hosted": subapp_hosted,
"link_tag": link_tag,
"no_tilde_in_html": "~" not in link_tag,
}
return {
"name": "href_browser_url_conversion",
"summary": "模拟 WebPage.Href() 将 ~ 虚拟路径转换为浏览器可识别的根目录或子应用 URL",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_logical_folders_and_url_mapping():
folders = {
"Account": "登录与安全文件",
"App_Data": "数据库与数据文件(禁止直接 HTTP 下载)",
"Images": "网站图片资源",
"Scripts": "浏览器端脚本",
"Shared": "布局页与公共样式等共享文件",
}
mapping = {
"URL": "https://example.com/html/html-tutorial.html",
"ServerName": "EXAMPLE-WEB",
"VirtualPath": "/html/html-tutorial.html",
"PhysicalPath": map_path(r"C:\MyWebSites\example", "/html/html-tutorial.html"),
}
expected = {
"folder_count": 5,
"virtual_path": "/html/html-tutorial.html",
"physical_path": r"C:\MyWebSites\example\html\html-tutorial.html",
}
actual = {
"folder_count": len(folders),
"virtual_path": mapping["VirtualPath"],
"physical_path": mapping["PhysicalPath"],
}
return {
"name": "logical_folders_and_url_mapping",
"summary": "核对典型 ASP.NET 网站五大逻辑文件夹职责及 URL、服务器名、虚拟路径与物理路径映射关系",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_tilde_virtual_paths(),
case_server_mappath_and_traversal_guard(),
case_href_browser_url_conversion(),
case_logical_folders_and_url_mapping(),
]
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-folders-lab/explore-webpages-folders.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-webpages-folders.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 ASP.NET WebPages / Razor 语义验证器(本机未启动 IIS / IIS Express / WebMatrix 宿主环境)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联55章) · 含当前章节或实例 · mbskycom202612a2981.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a3005.zip