--- title: "Introduction to ASP.NET Web Programming Using the Razor Syntax (C#) | Microsoft Docs" author: tfitzmac description: "This chapter gives you an overview of programming with ASP.NET Web Pages using the Razor syntax. ASP.NET is Microsoft's technology for running dynamic web pa..." ms.author: aspnetcontent manager: wpickett ms.date: 02/07/2014 ms.topic: article ms.assetid: aa67d304-583b-4bf8-a231-195656cfb587 ms.technology: dotnet-webpages ms.prod: .net-framework msc.legacyurl: /web-pages/overview/getting-started/introducing-razor-syntax-c msc.type: authoredcontent --- Introduction to ASP.NET Web Programming Using the Razor Syntax (C#) ==================== by [Tom FitzMacken](https://github.com/tfitzmac) > This article gives you an overview of programming with ASP.NET Web Pages using the Razor syntax. ASP.NET is Microsoft's technology for running dynamic web pages on web servers. This articles focuses on using the C# programming language. > > **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. ## 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. > [!NOTE] > The Razor syntax is based on the C# programming language, and that's the language that's used most often with ASP.NET Web Pages. However, the Razor syntax also supports the Visual Basic language, and everything you see you can also do in Visual Basic. For details, see the appendix [Visual Basic Language and Syntax](https://go.microsoft.com/fwlink/?LinkId=202908). You can find more details about most of these programming techniques later in the article. ### 1. You add code to a page using the @ character The `@` character starts inline expressions, single statement blocks, and multi-statement blocks: [!code-html[Main](introducing-razor-syntax-c/samples/sample1.html)] This is what these statements look like when the page runs in a browser: ![Razor-Img1](introducing-razor-syntax-c/_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 Forms](https://go.microsoft.com/fwlink/?LinkId=202892). ### 2. You enclose code blocks in braces A *code block* includes one or more code statements and is enclosed in braces. [!code-html[Main](introducing-razor-syntax-c/samples/sample2.html)] The result displayed in a browser: ![Razor-Img2](introducing-razor-syntax-c/_static/image2.jpg) ### 3. Inside a block, you end each code statement with a semicolon Inside a code block, each complete code statement must end with a semicolon. Inline expressions don't end with a semicolon. [!code-html[Main](introducing-razor-syntax-c/samples/sample3.html)] ### 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 `var` keyword. You can insert variable values directly in a page using `@`. [!code-html[Main](introducing-razor-syntax-c/samples/sample4.html)] The result displayed in a browser: ![Razor-Img3](introducing-razor-syntax-c/_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-cshtml[Main](introducing-razor-syntax-c/samples/sample5.cshtml)] If the string that you want to display contains a backslash character (\) or double quotation marks ( `"` ), use a *verbatim string literal* that's prefixed with the `@` operator. (In C#, the \ character has special meaning unless you use a verbatim string literal.) [!code-html[Main](introducing-razor-syntax-c/samples/sample6.html)] To embed double quotation marks, use a verbatim string literal and repeat the quotation marks: [!code-html[Main](introducing-razor-syntax-c/samples/sample7.html)] Here's the result of using both of these examples in a page: ![Razor-Img4](introducing-razor-syntax-c/_static/image4.jpg) > [!NOTE] > Notice that the `@` character is used both to mark verbatim string literals in C# and to mark code in ASP.NET pages. ### 6. Code is case sensitive In C#, keywords (like `var`, `true`, and `if`) and variable names are case sensitive. The following lines of code create two different variables, `lastName` and `LastName.` [!code-cshtml[Main](introducing-razor-syntax-c/samples/sample8.cshtml)] If you declare a variable as `var lastName = "Smith";` and if you try to reference that variable in your page as `@LastName`, an error results because `LastName` won't be recognized. > [!NOTE] > In Visual Basic, keywords and variables are *not* case sensitive. ### 7. Much of your coding involves 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 and that you can read or change — a text box object has a `Text` property (among others), 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 text boxes (form fields) on the page, what type of browser made the request, the URL of the page, the user identity, etc. The following 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-c/samples/sample9.html)] The result displayed in a browser: ![Razor-Img5](introducing-razor-syntax-c/_static/image5.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-cshtml[Main](introducing-razor-syntax-c/samples/sample10.cshtml)] 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-c/_static/image6.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 a submit button, 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.cshtml*. 2. Copy the following code and markup into the page, replacing anything already in the page. [!code-cshtml[Main](introducing-razor-syntax-c/samples/sample11.cshtml)] 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 that's embedded near the bottom of the page. - The block at the top of the page is enclosed in braces. - In the block at the top, all lines end with a semicolon. - 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 the code is case-sensitive, when the `totalMessage` variable is used near the bottom of the page, its name must match the variable at the top exactly. - 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 number (an integer) so that you can perform arithmetic on it. - The `