--- uid: web-pages/overview/getting-started/introducing-razor-syntax-vb title: "Introduction to ASP.NET Web Programming Using the Razor Syntax (Visual Basic) | Microsoft Docs" author: tfitzmac description: "This appendix gives you an overview of programming with ASP.NET Web pages in Visual Basic, using the Razor syntax." ms.author: aspnetcontent manager: wpickett ms.date: 02/07/2014 ms.topic: article ms.assetid: 5da59646-e973-41cd-88a9-c6b2c0594027 ms.technology: dotnet-webpages ms.prod: .net-framework msc.legacyurl: /web-pages/overview/getting-started/introducing-razor-syntax-vb msc.type: authoredcontent --- Introduction to ASP.NET Web Programming Using the Razor Syntax (Visual Basic) ==================== by [Tom FitzMacken](https://github.com/tfitzmac) > This article gives you an overview of programming with ASP.NET Web Pages using the Razor syntax and Visual Basic. ASP.NET is Microsoft's technology for running dynamic web pages on web servers. > > **What you'll learn**: > > - The top 8 programming tips for getting started with programming ASP.NET Web Pages using Razor syntax. > - Basic programming concepts you'll need. > - What ASP.NET server code and the Razor syntax is all about. > > > ## Software versions > > > - ASP.NET Web Pages (Razor) 3 > > > This tutorial also works with ASP.NET Web Pages 2. Most examples of using ASP.NET Web Pages with Razor syntax use C#. But the Razor syntax also supports Visual Basic. To program an ASP.NET web page in Visual Basic, you create a web page with a *.vbhtml* filename extension, and then add Visual Basic code. This article gives you an overview of working with the Visual Basic language and syntax to create ASP.NET Webpages. > [!NOTE] > The default website templates for Microsoft WebMatrix (**Bakery**, **Photo Gallery**, and **Starter Site**, etc.) are available in C# and Visual Basic versions. You can install the Visual Basic templates by as NuGet packages. Website templates are installed in the root folder of your site in a folder named *Microsoft Templates*. ## The Top 8 Programming Tips This section lists a few tips that you absolutely need to know as you start writing ASP.NET server code using the Razor syntax. ### 1. You add code to a page using the @ character The `@` character starts inline expressions, single-statement blocks, and multi-statement blocks: [!code-vbhtml[Main](introducing-razor-syntax-vb/samples/sample1.vbhtml)] The result displayed in a browser: ![Razor-Img1](introducing-razor-syntax-vb/_static/image1.jpg) > [!TIP] > > **HTML Encoding** > > When you display content in a page using the `@` character, as in the preceding examples, ASP.NET HTML-encodes the output. This replaces reserved HTML characters (such as `<` and `>` and `&`) with codes that enable the characters to be displayed as characters in a web page instead of being interpreted as HTML tags or entities. Without HTML encoding, the output from your server code might not display correctly, and could expose a page to security risks. > > If your goal is to output HTML markup that renders tags as markup (for example `
` for a paragraph or `` to emphasize text), see the section [Combining Text, Markup, and Code in Code Blocks](#BM_CombiningTextMarkupAndCode) later in this article. > > You can read more about HTML encoding in [Working with HTML Forms in ASP.NET Web Pages Sites](https://go.microsoft.com/fwlink/?LinkId=202892). ### 2. You enclose code blocks with Code...End Code A code block includes one or more code statements and is enclosed with the keywords `Code` and `End Code`. Place the opening `Code` keyword immediately after the `@` character — there can't be whitespace between them. [!code-vbhtml[Main](introducing-razor-syntax-vb/samples/sample2.vbhtml)] The result displayed in a browser: ![Razor-Img2](introducing-razor-syntax-vb/_static/image2.jpg) ### 3. Inside a block, you end each code statement with a line break In a Visual Basic code block, each statement ends with a line break. (Later in the article you'll see a way to wrap a long code statement into multiple lines if needed.) [!code-vbhtml[Main](introducing-razor-syntax-vb/samples/sample3.vbhtml)] ### 4. You use variables to store values You can store values in a *variable*, including strings, numbers, and dates, etc. You create a new variable using the `Dim` keyword. You can insert variable values directly in a page using `@`. [!code-vbhtml[Main](introducing-razor-syntax-vb/samples/sample4.vbhtml)] The result displayed in a browser: ![Razor-Img3](introducing-razor-syntax-vb/_static/image3.jpg) ### 5. You enclose literal string values in double quotation marks A *string* is a sequence of characters that are treated as text. To specify a string, you enclose it in double quotation marks: [!code-vbhtml[Main](introducing-razor-syntax-vb/samples/sample5.vbhtml)] To embed double quotation marks within a string value, insert two double quotation mark characters. If you want the double quotation character to appear once in the page output, enter it as `""` within the quoted string, and if you want it to appear twice, enter it as `""""` within the quoted string. [!code-vbhtml[Main](introducing-razor-syntax-vb/samples/sample6.vbhtml)] The result displayed in a browser: ![Razor-Img4](introducing-razor-syntax-vb/_static/image4.jpg) ### 6. Visual Basic code is not case sensitive The Visual Basic language is not case sensitive. Programming keywords (like `Dim`, `If`, and `True`) and variable names (like `myString`, or `subTotal`) can be written in any case. The following lines of code assign a value to the variable `lastname` using a lowercase name, and then output the variable value to the page using an uppercase name. [!code-vbhtml[Main](introducing-razor-syntax-vb/samples/sample7.vbhtml)] The result displayed in a browser: ![vb-syntax-5](introducing-razor-syntax-vb/_static/image5.jpg) ### 7. Much of your coding involves working with objects An object represents a thing that you can program with — a page, a text box, a file, an image, a web request, an email message, a customer record (database row), etc. Objects have properties that describe their characteristics — a text box object has a `Text` property, a request object has a `Url` property, an email message has a `From` property, and a customer object has a `FirstName` property. Objects also have methods that are the "verbs" they can perform. Examples include a file object's `Save` method, an image object's `Rotate` method, and an email object's `Send` method. You'll often work with the `Request` object, which gives you information like the values of form fields on the page (text boxes, etc.), what type of browser made the request, the URL of the page, the user identity, etc. This example shows how to access properties of the `Request` object and how to call the `MapPath` method of the `Request` object, which gives you the absolute path of the page on the server: [!code-html[Main](introducing-razor-syntax-vb/samples/sample8.html)] The result displayed in a browser: ![Razor-Img5](introducing-razor-syntax-vb/_static/image6.jpg) ### 8. You can write code that makes decisions A key feature of dynamic web pages is that you can determine what to do based on conditions. The most common way to do this is with the `If` statement (and optional `Else` statement). [!code-vbhtml[Main](introducing-razor-syntax-vb/samples/sample9.vbhtml)] The statement `If IsPost` is a shorthand way of writing `If IsPost = True`. Along with `If` statements, there are a variety of ways to test conditions, repeat blocks of code, and so on, which are described later in this article. The result displayed in a browser (after clicking **Submit**): ![Razor-Img6](introducing-razor-syntax-vb/_static/image7.jpg) > [!TIP] > > **HTTP GET and POST Methods and the IsPost Property** > > The protocol used for web pages (HTTP) supports a very limited number of methods ("verbs") that are used to make requests to the server. The two most common ones are GET, which is used to read a page, and POST, which is used to submit a page. In general, the first time a user requests a page, the page is requested using GET. If the user fills in a form and then clicks **Submit**, the browser makes a POST request to the server. > > In web programming, it's often useful to know whether a page is being requested as a GET or as a POST so that you know how to process the page. In ASP.NET Web Pages, you can use the `IsPost` property to see whether a request is a GET or a POST. If the request is a POST, the `IsPost` property will return true, and you can do things like read the values of text boxes on a form. Many examples you'll see show you how to process the page differently depending on the value of `IsPost`. ## A Simple Code Example This procedure shows you how to create a page that illustrates basic programming techniques. In the example, you create a page that lets users enter two numbers, then it adds them and displays the result. 1. In your editor, create a new file and name it *AddNumbers.vbhtml*. 2. Copy the following code and markup into the page, replacing anything already in the page. [!code-vbhtml[Main](introducing-razor-syntax-vb/samples/sample10.vbhtml)] Here are some things for you to note: - The `@` character starts the first block of code in the page, and it precedes the `totalMessage` variable embedded near the bottom. - The block at the top of the page is enclosed in `Code...End Code`. - The variables `total`, `num1`, `num2`, and `totalMessage` store several numbers and a string. - The literal string value assigned to the `totalMessage` variable is in double quotation marks. - Because Visual Basic code is not case sensitive, when the `totalMessage` variable is used near the bottom of the page, its name only needs to match the spelling of the variable declaration at the top of the page. The casing doesn't matter. - The expression `num1.AsInt()` + `num2.AsInt()` shows how to work with objects and methods. The `AsInt` method on each variable converts the string entered by a user to a whole number (an integer) that can be added. - The `