"""Offline ASP.NET MVC layout & styling verification lab for Chapter 29 (mvc-layout)."""
import html
import json
import re
VERSION = "CPython 3.12.14 offline ASP.NET MVC Razor layout semantic verifier (IIS/ASP.NET host absent)"
def render_layout(title, body_html, app_root="/"):
def url_content(tilde_path):
assert tilde_path.startswith("~/")
prefix = app_root.rstrip("/")
return f"{prefix}/{tilde_path[2:]}"
def action_link(text, action, controller):
href = "/" if (controller == "Home" and action == "Index") else f"/{controller}/{action}"
return f'<a href="{href}">{html.escape(text)}</a>'
nav = (
'<ul id="menu">'
f'<li>{action_link("Home", "Index", "Home")}</li>'
f'<li>{action_link("Movies", "Index", "Movies")}</li>'
f'<li>{action_link("About", "About", "Home")}</li>'
"</ul>"
)
return (
"<!DOCTYPE html><html><head><meta charset=\"utf-8\" />"
f"<title>{html.escape(title)}</title>"
f'<link href="{url_content("~/Content/Site.css")}" rel="stylesheet" type="text/css" />'
f"</head><body>{nav}"
f'<section id="main">{body_html}<p>Copyright MvcDemo. All Rights Reserved.</p></section>'
"</body></html>"
)
def case_layout_cshtml_renderbody_and_xss_escape():
rendered = render_layout(
title='Home <script>alert(1)</script>',
body_html="<h1>Welcome</h1><p>Content body</p>",
)
expected = {
"title_escaped": "<title>Home <script>alert(1)</script></title>" in rendered,
"no_raw_script_tag": "<script>alert(1)</script>" not in rendered,
"body_inserted": '<section id="main"><h1>Welcome</h1><p>Content body</p>' in rendered,
}
actual = {
"title_escaped": "<title>Home <script>alert(1)</script></title>" in rendered,
"no_raw_script_tag": "<script>alert(1)</script>" not in rendered,
"body_inserted": '<section id="main"><h1>Welcome</h1><p>Content body</p>' in rendered,
}
return {
"name": "layout_cshtml_renderbody_and_xss_escape",
"summary": "模拟 _Layout.cshtml 的 @ViewBag.Title 默认 HTML 编码防 XSS 与 @RenderBody() 视图正文注入",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_url_content_and_actionlink_helpers():
rendered_root = render_layout("Demo", "<p>ok</p>", app_root="/")
rendered_sub = render_layout("Demo", "<p>ok</p>", app_root="/mvcdemo")
expected = {
"root_css_href": True,
"subapp_css_href": True,
"nav_links": [
'<a href="/">Home</a>',
'<a href="/Movies/Index">Movies</a>',
'<a href="/Home/About">About</a>',
],
}
actual = {
"root_css_href": 'href="/Content/Site.css"' in rendered_root,
"subapp_css_href": 'href="/mvcdemo/Content/Site.css"' in rendered_sub,
"nav_links": re.findall(r'<a href="[^"]+">[^<]+</a>', rendered_root),
}
return {
"name": "url_content_and_actionlink_helpers",
"summary": "验证 @Url.Content('~/Content/Site.css') 虚拟根路径解析与 @Html.ActionLink() 导航链接输出",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_site_css_rule_groups_check():
css = (
'body{font: "Trebuchet MS", Verdana, sans-serif;background-color:#5c87b2;color:#696969;}'
'h1{border-bottom:3px solid #cc9900;color:#996600;}'
'#main{padding:20px;background-color:#ffffff;border-radius:0 4px 4px 4px;}'
'ul#menu{padding:0px;position:relative;margin:0;}'
'ul#menu li{display:inline;}'
'ul#menu li a{background-color:#e8eef4;padding:10px 20px;border-radius:4px 4px 0 0;}'
'fieldset{padding-left:12px;}'
'input[type="text"], input[type="password"]{width:300px;}'
'table.data{border:1px solid #c3c3c3;border-collapse:collapse;width:100%;}'
)
selectors = ["body", "h1", "#main", "ul#menu", "fieldset", "table.data"]
expected = {
"all_core_selectors_present": True,
"balanced_braces": True,
"uses_border_radius": True,
}
actual = {
"all_core_selectors_present": all(sel in css for sel in selectors),
"balanced_braces": css.count("{") == css.count("}"),
"uses_border_radius": "border-radius" in css,
}
return {
"name": "site_css_rule_groups_check",
"summary": "校验 Site.css 中页面基础样式、菜单标签(ul#menu)、表单(fieldset)与数据表(table.data)规则完整性",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_viewstart_default_layout_injection():
viewstart_layout = "~/Views/Shared/_Layout.cshtml"
def resolve_layout(view_override=None, has_viewstart=True):
if view_override is not None:
return view_override
return viewstart_layout if has_viewstart else None
expected = {
"default_with_viewstart": "~/Views/Shared/_Layout.cshtml",
"explicit_override": "~/Views/Shared/_AdminLayout.cshtml",
"disabled_layout": "",
"without_viewstart": None,
}
actual = {
"default_with_viewstart": resolve_layout(),
"explicit_override": resolve_layout(view_override="~/Views/Shared/_AdminLayout.cshtml"),
"disabled_layout": resolve_layout(view_override=""),
"without_viewstart": resolve_layout(has_viewstart=False),
}
return {
"name": "viewstart_default_layout_injection",
"summary": "验证 _ViewStart.cshtml 自动为所有视图注入默认 Layout、单页覆盖及删除 _ViewStart 后的回退行为",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_layout_cshtml_renderbody_and_xss_escape(),
case_url_content_and_actionlink_helpers(),
case_site_css_rule_groups_check(),
case_viewstart_default_layout_injection(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 离线 ASP.NET Razor / MVC 语义验证器(未启动 Visual Studio / IIS / ASP.NET MVC 运行时宿主)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 离线 ASP.NET Razor / MVC 语义验证器(未启动 Visual Studio / IIS / ASP.NET MVC 运行时宿主)。
入口文件:projects/web-pages-mvc-layout-lab/explore-mvc-layout.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-mvc-layout.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 ASP.NET Razor / MVC 语义验证器(未启动 Visual Studio / IIS / ASP.NET MVC 运行时宿主)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联55章) · 含当前章节或实例 · mbskycom202612a3037.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a3044.zip