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

离线校验 Web Pages @DateTime.Now 渲染、默认 HTML 转义与 WebMatrix 组件迁移

PYTHONWindows x64、CPython 3.12.14 离线 ASP.NET WebPages / Razor 语义验证器(本机未启动 IIS / IIS Express / WebMatrix 宿主环境)。explore-webpages-intro.py
explore-webpages-intro.py123 行
"""Offline ASP.NET semantic verification lab for Chapter 03 (webpages-intro)."""
import html
import json
import re

VERSION = "CPython 3.12.14 offline ASP.NET WebPages/Core semantic verifier (IIS/WebMatrix absent)"


def render_inline_razor(template: str, context: dict) -> str:
    def repl(match):
        expr = match.group(1)
        val = context[expr]
        return html.escape(str(val), quote=True)
    return re.sub(r"@([A-Za-z_][A-Za-z0-9_.]*)", repl, template)


def case_hello_webpages_datetime_render():
    template = (
        "<html>\n"
        "<body>\n"
        "     <h1>Hello Web Pages</h1>\n"
        "     <p>The time is @DateTime.Now</p>\n"
        "</body>\n"
        "</html>"
    )
    rendered = render_inline_razor(template, {"DateTime.Now": "2026/12/01 09:30:00"})
    expected = {
        "has_server_token_in_output": False,
        "rendered_paragraph": "<p>The time is 2026/12/01 09:30:00</p>",
        "browser_sees_only_html": True,
    }
    actual = {
        "has_server_token_in_output": "@DateTime.Now" in rendered,
        "rendered_paragraph": rendered.splitlines()[3].strip(),
        "browser_sees_only_html": "<h1>Hello Web Pages</h1>" in rendered and "@" not in rendered,
    }
    return {
        "name": "hello_webpages_datetime_render",
        "summary": "模拟 Web Pages 服务器端求值 @DateTime.Now 并向浏览器返回纯 HTML 输出(隐藏服务端源码)",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_webpages_vs_mvc_webforms_strip():
    modes = ("Web Pages", "MVC", "Web Forms")
    traits = [
        "easy_to_learn",
        "single_page_centric",
        "similar_to_php_and_classic_asp",
        "vb_or_csharp_server_script",
        "full_html_css_js_control",
    ]
    expected = {"modes": ["Web Pages", "MVC", "Web Forms"], "trait_count": 5}
    actual = {"modes": list(modes), "trait_count": len(traits)}
    return {
        "name": "webpages_vs_mvc_webforms_strip",
        "summary": "校验 ASP.NET 三模式横栏条目与 Web Pages 单页面开发模式的五项核心特征",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_webmatrix_bundle_components():
    bundle = {
        "templates": "Web Pages templates & examples",
        "language": "Razor (C# / VB)",
        "web_server": "IIS Express",
        "database": "SQL Server Compact",
        "framework": "ASP.NET",
    }
    open_source_gallery = ["Umbraco", "DotNetNuke", "Drupal", "Joomla", "WordPress"]
    expected = {
        "component_count": 5,
        "web_server": "IIS Express",
        "database": "SQL Server Compact",
        "gallery_apps": 5,
        "eol_migrated_to": "ASP.NET Core Razor Pages + .NET SDK",
    }
    actual = {
        "component_count": len(bundle),
        "web_server": bundle["web_server"],
        "database": bundle["database"],
        "gallery_apps": len(open_source_gallery),
        "eol_migrated_to": "ASP.NET Core Razor Pages + .NET SDK",
    }
    return {
        "name": "webmatrix_bundle_components",
        "summary": "核对历史 WebMatrix 套件五大组件、开源应用库代表项目及现代 ASP.NET Core Razor Pages 迁移目标",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_razor_html_auto_encoding():
    template = "<p>Note: @userInput</p>"
    rendered = render_inline_razor(template, {"userInput": "<script>alert('x')</script>"})
    expected = "<p>Note: &lt;script&gt;alert(&#x27;x&#x27;)&lt;/script&gt;</p>"
    return {
        "name": "razor_html_auto_encoding",
        "summary": "校验 Razor 内联 @ 表达式默认执行 HTML 实体转义以防止脚本注入",
        "expected": expected,
        "actual": rendered,
        "passed": rendered == expected,
    }


def main():
    cases = [
        case_hello_webpages_datetime_render(),
        case_webpages_vs_mvc_webforms_strip(),
        case_webmatrix_bundle_components(),
        case_razor_html_auto_encoding(),
    ]
    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/aspnet-webpages-intro-lab/explore-webpages-intro.py

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

下载课程关联工程包:ASP.NET

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

下载课程关联工程包:ASP.NET

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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