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

离线校验 MVC 路由模板解析、Controller 后缀发现约定与 HomeController 视图映射

PYTHONWindows x64、CPython 3.12.14 离线 ASP.NET Razor / MVC 语义验证器(未启动 Visual Studio / IIS / ASP.NET MVC 运行时宿主)。explore-mvc-controllers.py
explore-mvc-controllers.py121 行
"""Offline ASP.NET MVC controllers & routing verification lab for Chapter 30 (mvc-controllers)."""
import json

VERSION = "CPython 3.12.14 offline ASP.NET MVC controller routing semantic verifier (IIS/ASP.NET host absent)"


def route_url(path, defaults=("Home", "Index")):
    parts = [p for p in path.strip("/").split("/") if p]
    controller = parts[0] if len(parts) >= 1 else defaults[0]
    action = parts[1] if len(parts) >= 2 else defaults[1]
    route_id = parts[2] if len(parts) >= 3 else None
    return {"controller": controller, "action": action, "id": route_id}


def case_mvc_route_template_dispatch():
    expected = {
        "root": {"controller": "Home", "action": "Index", "id": None},
        "about": {"controller": "Home", "action": "About", "id": None},
        "edit_movie": {"controller": "Movies", "action": "Edit", "id": "3"},
    }
    actual = {
        "root": route_url("/"),
        "about": route_url("/Home/About"),
        "edit_movie": route_url("/Movies/Edit/3"),
    }
    return {
        "name": "mvc_route_template_dispatch",
        "summary": "验证默认路由模板 {controller}/{action}/{id} 对根路径、/Home/About 与 /Movies/Edit/3 的解析分发",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_controller_suffix_convention_validation():
    candidates = ["HomeController", "AccountController", "MoviesController", "HelperService"]
    discovered = {c[: -len("Controller")]: c for c in candidates if c.endswith("Controller")}
    expected = {
        "discovered_count": 3,
        "home_class": "HomeController",
        "non_controller_ignored": True,
    }
    actual = {
        "discovered_count": len(discovered),
        "home_class": discovered.get("Home"),
        "non_controller_ignored": "HelperService" not in discovered.values(),
    }
    return {
        "name": "controller_suffix_convention_validation",
        "summary": "验证控制器类名必须以 Controller 结尾的框架发现约定,过滤非控制器类",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_home_controller_actions_and_view_resolution():
    actions = {
        "Home": {
            "Index": lambda: {"type": "ViewResult", "view_path": "Views/Home/Index.cshtml"},
            "About": lambda: {"type": "ViewResult", "view_path": "Views/Home/About.cshtml"},
        }
    }
    r1 = route_url("/")
    r2 = route_url("/Home/About")
    res1 = actions[r1["controller"]][r1["action"]]()
    res2 = actions[r2["controller"]][r2["action"]]()
    expected = {
        "index_view": "Views/Home/Index.cshtml",
        "about_view": "Views/Home/About.cshtml",
        "both_view_result": True,
    }
    actual = {
        "index_view": res1["view_path"],
        "about_view": res2["view_path"],
        "both_view_result": res1["type"] == "ViewResult" and res2["type"] == "ViewResult",
    }
    return {
        "name": "home_controller_actions_and_view_resolution",
        "summary": "验证 HomeController.Index() 与 HomeController.About() 返回 ActionResult/View() 到对应 .cshtml 的映射",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_file_mapping_vs_mvc_routing():
    traditional_url = "https://example.com/index.php"
    mvc_url = "https://example.com/Home/About"
    expected = {
        "traditional_maps_to": "disk_file:index.php",
        "mvc_maps_to": "method:HomeController.About()",
        "decouples_url_from_filesystem": True,
    }
    actual = {
        "traditional_maps_to": "disk_file:" + traditional_url.rsplit("/", 1)[-1],
        "mvc_maps_to": "method:" + route_url("/Home/About")["controller"] + "Controller." + route_url("/Home/About")["action"] + "()",
        "decouples_url_from_filesystem": not mvc_url.endswith(".cshtml"),
    }
    return {
        "name": "file_mapping_vs_mvc_routing",
        "summary": "对比传统物理文件 URL 映射(index.php)与 MVC 控制器方法路由映射的架构差异",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = [
        case_mvc_route_template_dispatch(),
        case_controller_suffix_convention_validation(),
        case_home_controller_actions_and_view_resolution(),
        case_file_mapping_vs_mvc_routing(),
    ]
    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-forms-mvc-controllers-lab/explore-mvc-controllers.py

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

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

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

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

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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