蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

离线校验 Razor 视图查找回退顺序、5 类文件扩展名及 Index/About 渲染与路由可达性

PYTHONWindows x64、CPython 3.12.14 离线 ASP.NET Razor / MVC 语义验证器(未启动 Visual Studio / IIS / ASP.NET MVC 运行时宿主)。explore-mvc-views.py
explore-mvc-views.py143 行
"""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/web-pages-mvc-views-lab/explore-mvc-views.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-mvc-views.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14 离线 ASP.NET Razor / MVC 语义验证器(未启动 Visual Studio / IIS / ASP.NET MVC 运行时宿主)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Web Pages

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a3048.zip

下载课程关联工程包:Web Pages

课程关联工程包(登记关联55章) · 含当前章节或实例 · mbskycom202612a3037.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习