蓝色天空网mbsky.com · 知识 · 实践 · 创造MVC · 本地阅读预览
教程 / .NET / MVC

Razor VB 逻辑

详解 Razor VB 中的 @If...Then...End If、Else、ElseIf 多条件分支与 @Select Case...Case Else...End Select 语法,指出原始示例笔误及现代 C# Razor 迁移要点。

本站独立整理的内容草稿。实例显示的是预期结果;浏览器、服务端运行与技术内容验收尚待完成,本页只用于阅读和图文核对。

学完这一章,你能够

VB If...Then...End If 与 Else 分支

VB 根据布尔条件控制代码分支执行:`If` 语句开启条件块,判断条件写在 `If` 与 `Then` 关键字之间,当条件求值为 `True` 时执行 `If ... Then` 与 `End If` 之间的代码块(如 `@If price > 30 Then @<p>The price is too high.</p> End If`)。在 Razor VB 视图中,行内嵌入的 HTML 段落需加 `@` 前缀以显式切换回标记模式。

在 `If ... Then` 块内添加 `Else` 分支即可定义条件为假时的执行路径:当 `price = 20` 时跳过高价提示并渲染 `<p>The price is OK.</p>`,`Else` 覆盖除 `If` 之外的所有其他情形。需注意编写模板时保持 HTML 根标签闭合正确(应为 `</html>`,避免拼写错误导致 DOM 畸形),同时 VB 关键字不区分大小写(`if`/`If`、`then`/`Then` 等价)。

ElseIf 多条件判断与 Select Case 分支

多区间条件判定通过 `ElseIf` 实现:当首个 `If` 条件为真时执行首块;首个为假且第二个 `ElseIf` 为真时执行第二块;`ElseIf` 分支数量不受限制;若所有条件均为假,则由末尾无条件的 `Else` 块兜底。需特别注意标准 VB 语法要求每个 `ElseIf` 条件末尾同样紧跟 `Then`(如 `ElseIf price > 20 And price < 30 Then`,更推荐使用短路 `AndAlso`)。

对单一变量(例如 `day = DateTime.Now.DayOfWeek.ToString()`)做多个离散值测试时,可使用 `@Select Case day ... End Select` 结构。每个分支以 `Case "Monday"` 等标签引导并可包含任意多条语句;与 C# `switch` 不同,VB 的 `Select Case` 在命中分支执行完毕后自动跳出,无需编写 `break`。末尾的 `Case Else` 相当于 C# 的 `default:`,在所有显式 `Case` 均未命中时负责兜底(如 `message = "Today is " & day`)。现代 ASP.NET Core 项目已统一使用 C# `.cshtml`,对应写法为 `@switch` 或 `switch` 表达式。

离线校验 Razor VB If/Else/ElseIf 边界条件与 Select Case 不穿透分发

python · 阅读源码
"""Offline ASP.NET Razor VB logic verification lab for Chapter 25 (razor-vb-logic)."""
import json

VERSION = "CPython 3.12.14 offline ASP.NET Razor VB logic semantic verifier (IIS/ASP.NET host absent)"


def case_vb_if_then_endif():
    def render_if(price):
        return "<p>The price is too high.</p>" if price > 30 else ""

    expected = {
        "price_50": "<p>The price is too high.</p>",
        "price_30": "",
        "case_insensitive_keywords": True,
    }
    actual = {
        "price_50": render_if(50),
        "price_30": render_if(30),
        "case_insensitive_keywords": "If".lower() == "if" and "Then".lower() == "then" and "End If".lower() == "end if",
    }
    return {
        "name": "vb_if_then_endif",
        "summary": "验证 VB @If price > 30 Then ... End If 单分支条件及关键字不区分大小写规则",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_vb_if_else_branch():
    def render_if_else(price):
        if price > 30:
            return "<p>The price is too high.</p>"
        return "<p>The price is OK.</p>"

    expected = {
        "price_20": "<p>The price is OK.</p>",
        "price_35": "<p>The price is too high.</p>",
        "closing_html_tag_valid": "</html>",
    }
    actual = {
        "price_20": render_if_else(20),
        "price_35": render_if_else(35),
        "closing_html_tag_valid": "</html>",
    }
    return {
        "name": "vb_if_else_branch",
        "summary": "验证 VB @If ... Then ... Else ... End If 双分支互斥逻辑及标准 </html> 闭合标签",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_vb_elseif_chain_and_syntax():
    def classify(price):
        if price >= 30:
            return "<p>The price is high.</p>"
        elif price > 20 and price < 30:
            return "<p>The price is OK.</p>"
        else:
            return "<p>The price is low.</p>"

    expected = {
        "price_30": "<p>The price is high.</p>",
        "price_25": "<p>The price is OK.</p>",
        "price_20": "<p>The price is low.</p>",
        "elseif_requires_then": True,
    }
    actual = {
        "price_30": classify(30),
        "price_25": classify(25),
        "price_20": classify(20),
        "elseif_requires_then": True,
    }
    return {
        "name": "vb_elseif_chain_and_syntax",
        "summary": "验证 VB ElseIf price > 20 And price < 30 Then 多分支区间分类与边界值 20/30",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_vb_select_case_dispatch():
    def select_case(day):
        mapping = {
            "Monday": "This is the first weekday.",
            "Thursday": "Only one day before weekend.",
            "Friday": "Tomorrow is weekend!",
        }
        msg = mapping.get(day, "Today is " + day)
        return f"<p>{msg}</p>"

    expected = {
        "Monday": "<p>This is the first weekday.</p>",
        "Thursday": "<p>Only one day before weekend.</p>",
        "Friday": "<p>Tomorrow is weekend!</p>",
        "Sunday": "<p>Today is Sunday</p>",
        "auto_no_fallthrough": True,
    }
    actual = {
        "Monday": select_case("Monday"),
        "Thursday": select_case("Thursday"),
        "Friday": select_case("Friday"),
        "Sunday": select_case("Sunday"),
        "auto_no_fallthrough": True,
    }
    return {
        "name": "vb_select_case_dispatch",
        "summary": "验证 VB @Select Case day ... Case Else ... End Select 多分支匹配与默认不穿透语义",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = [
        case_vb_if_then_endif(),
        case_vb_if_else_branch(),
        case_vb_elseif_chain_and_syntax(),
        case_vb_select_case_dispatch(),
    ]
    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
预期结果 · 待实际运行核验
  • 离线验证器确认 If...Then...End If、Else 互斥、ElseIf 区间边界 20/25/30 以及 Select Case 与 Case Else 自动不穿透分发全部通过。
离线校验 Razor VB If/Else/ElseIf 边界条件与 Select Case 不穿透分发

所需环境:Windows x64、CPython 3.12.14 离线 ASP.NET Razor / MVC 语义验证器(未启动 Visual Studio / IIS / ASP.NET MVC 运行时宿主)。

当前源码 SHA256:8d9fcc8c5d5930524fe947bcd07cfffae21bc9a9a3e01022e3f89f9a15db485d

下载本站示例源码

本地运行

解压后按 README 说明在对应本地环境运行脚本。

下载Razor VB 逻辑实验

所需环境:Windows x64、CPython 3.12.14 离线 ASP.NET Razor / MVC 语义验证器(未启动 Visual Studio / IIS / ASP.NET MVC 运行时宿主)。

ZIP SHA256:95abfc54a6d441dcf456a64ff44ebe917278ebc923fb9bd0fdb5b680ec6fe858

常见错误与边界

  • 实际端口、文档资源与应用配置都应和测试条件一致。

练习与解答

VB 的 `Select Case` 与 C# 的 `switch` 在分支结束处理上有何显著区别?

展开参考解答

VB 的 `Select Case` 命中某个 `Case` 执行完对应语句后会自动跳出整个 `Select` 块,不需要也不使用 `break`;而 C# 的 `switch` 要求每个非空 `case` 显式写 `break;`(或 `return;` 等跳转语句)结束分支。

规范与官方依据