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

离线校验静态页升级 Razor、C# @{ } 与 VB @Code 语法规则及大小写差异

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

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


def case_static_html_to_razor_datetime():
    plain_html = (
        '<!DOCTYPE html>\n<html lang="en">\n<head>\n'
        '<meta charset="utf-8" />\n<title>Web Pages Demo</title>\n</head>\n'
        '<body>\n<h1>Hello Web Pages</h1>\n</body>\n</html>'
    )
    razor_html = plain_html.replace(
        "<h1>Hello Web Pages</h1>",
        "<h1>Hello Web Pages</h1>\n<p>The time is @DateTime.Now</p>",
    )
    fixed_ts = "2026-12-01 10:00:00"
    rendered = razor_html.replace("@DateTime.Now", fixed_ts)
    expected = {
        "plain_has_at": False,
        "razor_has_at": True,
        "rendered_contains_ts": True,
        "formatted_iso_date": "2026-12-01",
    }
    actual = {
        "plain_has_at": "@" in plain_html,
        "razor_has_at": "@DateTime.Now" in razor_html,
        "rendered_contains_ts": f"<p>The time is {fixed_ts}</p>" in rendered,
        "formatted_iso_date": fixed_ts.split()[0],
    }
    return {
        "name": "static_html_to_razor_datetime",
        "summary": "对比纯 HTML 页面与添加 @DateTime.Now 后的 Razor 动态页面服务器端渲染结果",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_razor_csharp_blocks_and_inline():
    # Simulates C# Razor rules: @{ ... }, var, semicolon-terminated statements, case-sensitive
    env = {}
    env["myMessage"] = "Hello World"
    env["greeting"] = "Welcome to our site!"
    env["weekDay"] = "Tuesday"
    env["greetingMessage"] = env["greeting"] + " Today is: " + env["weekDay"]

    out_p1 = f"<p>The value of myMessage is: {html.escape(env['myMessage'])}</p>"
    out_p2 = f"<p>The greeting is: {html.escape(env['greetingMessage'])}</p>"
    case_sensitive_distinct = ("myMessage" in env) and ("mymessage" not in env)
    expected = {
        "extension": ".cshtml",
        "p1": "<p>The value of myMessage is: Hello World</p>",
        "p2": "<p>The greeting is: Welcome to our site! Today is: Tuesday</p>",
        "case_sensitive": True,
    }
    actual = {
        "extension": ".cshtml",
        "p1": out_p1,
        "p2": out_p2,
        "case_sensitive": case_sensitive_distinct,
    }
    return {
        "name": "razor_csharp_blocks_and_inline",
        "summary": "验证 Razor C#(.cshtml)单语句块、多语句块 @{ ... }、var 变量、+ 拼接与区分大小写规则",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_razor_vb_blocks_and_inline():
    # Simulates VB Razor rules: @Code ... End Code, Dim, & concatenation, case-insensitive lookup
    vb_store = {}

    def vb_set(name, value):
        vb_store[name.lower()] = value

    def vb_get(name):
        return vb_store[name.lower()]

    vb_set("myMessage", "Hello World")
    vb_set("greeting", "Welcome to our site!")
    vb_set("weekDay", "Tuesday")
    vb_set("greetingMessage", vb_get("greeting") + " Today is: " + vb_get("weekDay"))

    out_p1 = f"<p>The value of myMessage is: {html.escape(vb_get('MYMESSAGE'))}</p>"
    out_p2 = f"<p>The greeting is: {html.escape(vb_get('GreetingMessage'))}</p>"
    expected = {
        "extension": ".vbhtml",
        "p1": "<p>The value of myMessage is: Hello World</p>",
        "p2": "<p>The greeting is: Welcome to our site! Today is: Tuesday</p>",
        "case_insensitive": True,
    }
    actual = {
        "extension": ".vbhtml",
        "p1": out_p1,
        "p2": out_p2,
        "case_insensitive": vb_get("mymessage") == vb_get("MYMESSAGE"),
    }
    return {
        "name": "razor_vb_blocks_and_inline",
        "summary": "验证 Razor VB(.vbhtml)@Code ... End Code 块、Dim 声明、& 拼接与不区分大小写规则",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_csharp_vs_vb_rule_table():
    cs_rules = {
        "block": "@{ ... }",
        "inline": "@",
        "terminator": ";",
        "var_keyword": "var",
        "case_sensitive": True,
        "ext": ".cshtml",
        "aspnet_core_supported": True,
    }
    vb_rules = {
        "block": "@Code ... End Code",
        "inline": "@",
        "terminator": "newline",
        "var_keyword": "Dim",
        "case_sensitive": False,
        "ext": ".vbhtml",
        "aspnet_core_supported": False,
    }
    expected = {
        "shared_inline_prefix": "@",
        "cs_semicolon": True,
        "vb_semicolon": False,
        "core_uses_cshtml_only": True,
    }
    actual = {
        "shared_inline_prefix": cs_rules["inline"] if cs_rules["inline"] == vb_rules["inline"] else "",
        "cs_semicolon": cs_rules["terminator"] == ";",
        "vb_semicolon": vb_rules["terminator"] == ";",
        "core_uses_cshtml_only": cs_rules["aspnet_core_supported"] and not vb_rules["aspnet_core_supported"],
    }
    return {
        "name": "csharp_vs_vb_rule_table",
        "summary": "对比 Razor C# 与 Razor VB 语法差异并校验 ASP.NET Core 仅原生支持 C# .cshtml 视图的演进边界",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = [
        case_static_html_to_razor_datetime(),
        case_razor_csharp_blocks_and_inline(),
        case_razor_vb_blocks_and_inline(),
        case_csharp_vs_vb_rule_table(),
    ]
    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/web-forms-webpages-razor-lab/explore-webpages-razor.py

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

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

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

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

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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