What it is
A .vbhtml file is Razor whose code is Visual Basic. It exists in ASP.NET Web Pages and in ASP.NET MVC 3 to 5 (the .NET Framework). ASP.NET Core does not compile it: Core has Razor for C# only. The markup side is the same as in .cshtml: the same email rule, @* comments, @@ escape, attributes, and <text>. What differs is how code is delimited, because Visual Basic has no braces and ends a line to end a statement.
@ModelType IEnumerable(Of Product)
@Code
ViewData("Title") = "Products"
End Code
<ul>
@For Each p In Model
@<li>@p.Name: @p.Price.ToString("C")</li>
Next
</ul>
@If Model.Any() Then
@:There are @Model.Count() products.
End If
Expressions
- Implicit:
@Model.Name,@Html.Raw(x),@items(0).Name. After the first word the parser takes a parenthesised group (balanced; strings and'comments inside are skipped) or a dot followed by a name, and repeats. Anything else ends it. A dot followed by@and a name is also taken. As in C#, a trailing dot is left as text. - Explicit:
@(a + b), to the matching parenthesis; if it never closes, the error is reported and the rest of the line is taken. - Generic arguments are written
(Of T), so the C# problem of@Foo<int>()does not arise. - There is no
@{ }block:@{is an error in Visual Basic Razor. Use@Code ... End Code.
Blocks end with End lines
A block starts with an @ and a Visual Basic word and ends at the matching terminator. The parser matches words case-insensitively, counts nested blocks of the same kind, and skips strings ("a ""End If"" b") and comments (' to the end of the line, and REM at the start of a statement), so an End If inside them does not end the block. Exit and Continue followed by the block's own word are not counted as a nested block, so Exit For inside a For does not open another.
| Block | Ends at | Holds |
|---|---|---|
@Code | End Code | Statements and markup (with @< or @:). |
@Functions | End Functions | Members of the page class; no markup. |
@Helper Name(parameters) | End Helper | A reusable piece of markup (the legacy Web Pages helper). The header is checked for the name and parentheses. |
@Section Name | End Section | Markup for the layout's section. Sections cannot nest. |
@If | End If | Statements and markup; Else, ElseIf inside. |
@Select Case | End Select | |
@While, @Using, @With, @SyncLock, @Try | End While, End Using, End With, End SyncLock, End Try | |
@For and @For Each | Next (the rest of that line belongs to the block) | |
@Do | Loop (with its While or Until clause) |
A block that never ends is reported as "The block is missing a closing End ... statement". A single-line If ... Then ... without an End If is counted as a nested block and so leaves the block open, as in the real parser.
Markup inside Visual Basic
In C# a tag at the start of a statement switches to markup; in Visual Basic there is no such rule. Markup inside a block is always introduced by an @:
@<tag>...</tag>: a markup block with matched tags, as in the example above.@:text: the rest of the line is markup.@<text>...</text>: a block of text with no tag of its own.@Nameor@(expr): a nested expression;@If,@Forand the other block words nest too.@*...*@: a Razor comment.
Directives
| Directive | Value | Effect |
|---|---|---|
@ModelType T | a type (to the end of the line) | The type of Model (MVC). |
@Imports X | a namespace or alias, to the end of the line | A Visual Basic Imports. |
@Inherits T | a type name, required | The base class of the page. |
@Option Strict Off, @Option Explicit On | Strict or Explicit, then On or Off | Compiler options for the page; another option name is reported as unknown. |
@Layout "~/_Layout.vbhtml" | a path | The layout page (Web Pages). |
@SessionState value | the rest of the line | Session state mode for the page (Web Pages). |
@Code, @Functions, @Section, @Helper | see above | |
@Class, @Namespace | Reserved: the error "is a reserved word and cannot be used in implicit expressions". |
Directive words are matched without regard to case in Visual Basic Razor (@Modeltype works).
Differences from C# Razor
| Subject | C# (.cshtml) | Visual Basic (.vbhtml) |
|---|---|---|
| Code block | @{ ... } | @Code ... End Code |
| Markup in a block | a tag at the start of a statement, @:, <text>, @<tag> | @<tag>, @:, @<text>; always with @ |
| Strings | "a\"b", verbatim, interpolated, raw | "a""b" only |
| Comments | //, /* */ | ', REM |
| Generics | M<int>(), needs @( ) | M(Of Integer)(), fine implicitly |
| Case of directives | case-sensitive | case-insensitive |
| Host | .NET Core, .NET Framework | .NET Framework only |
In the viewer
A .vbhtml file is read with these rules and coloured with the Visual Basic colouring for the code. A .cshtml file that holds Visual Basic Razor (it has an @ModelType or @Imports line, an @Code line, or an End If, End Code, End Using, End Section, End Functions, End Helper or End Select line, and no @model, @using, @inject or @page) is shown as the Visual Basic variant.