"""Offline ASP.NET MVC views verification lab for Chapter 31 (mvc-views)."""
import html
import json
VERSION = "CPython 3.12.14 offline ASP.NET MVC Razor view engine semantic verifier (IIS/ASP.NET host absent)"
def case_views_folder_and_search_order():
existing = {
"Views/Account/LogOn.cshtml",
"Views/Account/Register.cshtml",
"Views/Home/Index.cshtml",
"Views/Home/About.cshtml",
"Views/Shared/_Layout.cshtml",
"Views/Shared/Error.cshtml",
}
def find_view(controller, view_name):
candidates = [
f"Views/{controller}/{view_name}.cshtml",
f"Views/Shared/{view_name}.cshtml",
]
for path in candidates:
if path in existing:
return path
return None
expected = {
"home_index": "Views/Home/Index.cshtml",
"home_about": "Views/Home/About.cshtml",
"shared_fallback": "Views/Shared/Error.cshtml",
"missing_movies": None,
}
actual = {
"home_index": find_view("Home", "Index"),
"home_about": find_view("Home", "About"),
"shared_fallback": find_view("Home", "Error"),
"missing_movies": find_view("Movies", "Index"),
}
return {
"name": "views_folder_and_search_order",
"summary": "模拟 Razor 视图引擎按 Views/{Controller}/{Action}.cshtml 到 Views/Shared/{Action}.cshtml 的回退查找顺序",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_aspnet_view_file_extensions_table():
file_types = [
("纯 HTML", [".htm", ".html"]),
("经典 ASP", [".asp"]),
("经典 ASP.NET", [".aspx"]),
("ASP.NET Razor C#", [".cshtml"]),
("ASP.NET Razor VB", [".vbhtml"]),
]
expected = {
"type_count": 5,
"razor_cs_ext": ".cshtml",
"razor_vb_ext": ".vbhtml",
"core_supported_razor": [".cshtml"],
}
actual = {
"type_count": len(file_types),
"razor_cs_ext": file_types[3][1][0],
"razor_vb_ext": file_types[4][1][0],
"core_supported_razor": [ext for name, exts in file_types if "Razor C#" in name for ext in exts],
}
return {
"name": "aspnet_view_file_extensions_table",
"summary": "核对 Views 文件夹中 5 类 HTML/ASP.NET 扩展名及 ASP.NET Core 仅保留 .cshtml 的演进规则",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_index_and_about_viewbag_render():
def render_with_layout(viewbag_title, h1_text, p_text):
return (
f"<title>{html.escape(viewbag_title)}</title>"
f"<section id=\"main\"><h1>{html.escape(h1_text)}</h1><p>{html.escape(p_text)}</p></section>"
)
index_html = render_with_layout("Home Page", "Welcome to MvcDemo", "Put Home Page content here")
about_html = render_with_layout("About Us", "About Us", "Put About Us content here")
expected = {
"index_title_ok": "<title>Home Page</title>" in index_html,
"index_h1_ok": "<h1>Welcome to MvcDemo</h1>" in index_html,
"about_title_ok": "<title>About Us</title>" in about_html,
"about_h1_ok": "<h1>About Us</h1>" in about_html,
}
actual = {
"index_title_ok": "<title>Home Page</title>" in index_html,
"index_h1_ok": "<h1>Welcome to MvcDemo</h1>" in index_html,
"about_title_ok": "<title>About Us</title>" in about_html,
"about_h1_ok": "<h1>About Us</h1>" in about_html,
}
return {
"name": "index_and_about_viewbag_render",
"summary": "验证 Index.cshtml 与 About.cshtml 通过 ViewBag.Title 向 _Layout.cshtml 传递标题并渲染正文",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_unwired_movies_route_status():
registered_controllers = {"Home": {"Index", "About"}, "Account": {"LogOn", "Register"}}
def simulate_click(controller, action="Index"):
if controller not in registered_controllers or action not in registered_controllers[controller]:
return 404
return 200
expected = {"home_status": 200, "about_status": 200, "movies_status_before_model": 404}
actual = {
"home_status": simulate_click("Home", "Index"),
"about_status": simulate_click("Home", "About"),
"movies_status_before_model": simulate_click("Movies", "Index"),
}
return {
"name": "unwired_movies_route_status",
"summary": "验证首次运行 MvcDemo 时 Home/About 可达(200),而尚未创建 MoviesController 的 Movies 标签返回 404",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_views_folder_and_search_order(),
case_aspnet_view_file_extensions_table(),
case_index_and_about_viewbag_render(),
case_unwired_movies_route_status(),
]
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/razor-mvc-views-lab/explore-mvc-views.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-mvc-views.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 ASP.NET Razor / MVC 语义验证器(未启动 Visual Studio / IIS / ASP.NET MVC 运行时宿主)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联55章) · 含当前章节或实例 · mbskycom202612a2925.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2940.zip