Razor VB 循环
详解 Razor VB 中 @For...To...Next 闭区间循环、@For Each...In...Next 遍历、Do While...Loop 条件循环与 String() 数组圆括号下标访问,并对比其与 C# 循环边界差异。
学完这一章,你能够
- 详解 Razor VB 中 @For...To...Next 闭区间循环、@For Each...In...Next 遍历、Do While...Loop 条件循环与 String() 数组圆括号下标访问,并对比其与 C# 循环边界差异。
VB For 闭区间循环与 For Each 遍历
在 Razor VB(`.vbhtml`)中需要按已知次数重复执行语句时,可使用 `@For i = 10 To 21` 配合 `Next i` 结束循环体,并在循环内以 `@<p>Line #@i</p>` 输出单行内联 HTML 标记。需重点辨析与 C# 的边界差异:VB 的 `For i = 10 To 21` 属于**两端闭区间**,会依次输出 `Line #10` 直至 `Line #21` 共 12 次迭代(而 C# 的 `i < 21` 仅执行 11 次)。
遍历数组或集合时,使用 `@For Each x In Request.ServerVariables` 配合 `@<li>@x</li>` 与 `Next x` 即可完整迭代集合成员并输出列表项。同样地,`Request.ServerVariables` 与 `.vbhtml` 均属传统 ASP.NET Framework 范畴,在现代 ASP.NET Core 中已演进为 C# Razor 视图搭配 `HttpContext.Request.Headers` 等强类型接口。
Do While 通用循环与 VB 数组操作
通用条件循环在 VB 中通常写作 `Do While i < 5 ... Loop`(亦可写作 `While ... End While`,无需像 C# 那样强加圆括号,循环块包裹在 `@Code ... End Code` 内)。在每轮循环中通过 `i += 1` 递增计数器并以 `@<p>Line #@i</p>` 输出标记,循环执行 5 次并渲染 `Line #1` 到 `Line #5`。
需要成组保存多个同类数据时,可在 `@Code` 块中声明字符串数组:`Dim members As String() = {"Jani", "Hege", "Kai", "Jim"}`。VB 数组在访问元素时使用**圆括号**而非方括号,且同样为 0 基下标:`members(2 - 1)` 取得第 2 个成员 `"Hege"`;`members.Length` 返回元素个数 `4`;`Array.IndexOf(members, "Kai") + 1` 计算出 `"Kai"` 所在的 1 基位置 `3`;随后可用 `@For Each person In members ... Next person` 逐项输出成员列表。
离线校验 Razor VB 闭区间 For 循环、Do While 与圆括号数组索引
展开查看收起
"""Offline ASP.NET Razor VB loops & arrays verification lab for Chapter 24 (razor-vb-loops)."""
import html
import json
VERSION = "CPython 3.12.14 offline ASP.NET Razor VB loops semantic verifier (IIS/ASP.NET host absent)"
def case_vb_for_to_inclusive_loop():
# VB "For i = 10 To 21 ... Next i" is inclusive on both bounds (12 iterations)
items = [f"<p>Line #{i}</p>" for i in range(10, 21 + 1)]
expected = {
"count": 12,
"first": "<p>Line #10</p>",
"last": "<p>Line #21</p>",
"inclusive_upper_bound": True,
}
actual = {
"count": len(items),
"first": items[0],
"last": items[-1],
"inclusive_upper_bound": items[-1] == "<p>Line #21</p>",
}
return {
"name": "vb_for_to_inclusive_loop",
"summary": "验证 VB @For i = 10 To 21 闭区间循环共执行 12 次(含 21),与 C# i < 21 的 11 次形成对比",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_vb_foreach_server_variables():
server_vars = ["HTTP_HOST", "REQUEST_METHOD", "SERVER_PORT", "URL"]
rendered = "<ul>" + "".join(f"<li>{html.escape(k)}</li>" for k in server_vars) + "</ul>"
expected = {
"item_count": 4,
"rendered": "<ul><li>HTTP_HOST</li><li>REQUEST_METHOD</li><li>SERVER_PORT</li><li>URL</li></ul>",
}
actual = {
"item_count": len(server_vars),
"rendered": rendered,
}
return {
"name": "vb_foreach_server_variables",
"summary": "模拟 VB @For Each x In Request.ServerVariables ... Next x 遍历并渲染无序列表",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_vb_do_while_loop_render():
i = 0
lines = []
while i < 5:
i += 1
lines.append(f"<p>Line #{i}</p>")
expected = {
"final_i": 5,
"lines": ["<p>Line #1</p>", "<p>Line #2</p>", "<p>Line #3</p>", "<p>Line #4</p>", "<p>Line #5</p>"],
}
actual = {
"final_i": i,
"lines": lines,
}
return {
"name": "vb_do_while_loop_render",
"summary": "验证 VB Do While i < 5 ... Loop 配合 @<p> 内联标记逐次递增输出 1..5",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_vb_array_parenthesis_indexing():
members = ["Jani", "Hege", "Kai", "Jim"]
pos_kai = members.index("Kai") + 1
length = len(members)
second_person = members[2 - 1] # VB members(2-1)
expected = {
"length": 4,
"person_at_position_2": "Hege",
"kai_1based_position": 3,
"paragraphs": ["<p>Jani</p>", "<p>Hege</p>", "<p>Kai</p>", "<p>Jim</p>"],
}
actual = {
"length": length,
"person_at_position_2": second_person,
"kai_1based_position": pos_kai,
"paragraphs": [f"<p>{p}</p>" for p in members],
}
return {
"name": "vb_array_parenthesis_indexing",
"summary": "验证 VB String() 数组声明、圆括号下标 members(2-1)、Length 属性与 Array.IndexOf 定位",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_vb_for_to_inclusive_loop(),
case_vb_foreach_server_variables(),
case_vb_do_while_loop_render(),
case_vb_array_parenthesis_indexing(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
本地运行
解压后按 README 说明在对应本地环境运行脚本。
本章附件与实例包
常见错误与边界检查
- 实际端口、文档资源与应用配置都应和测试条件一致。
技术规范与官方资料
练习与参考解答
查看参考解答
因为 VB 的 `To` 上界是包含(inclusive)的,循环变量会取到 21 并执行循环体(共 12 次);而 C# 中的 `i < 21` 是严格小于条件,当 `i` 增至 21 时即退出循环(共 11 次)。