YouTip LogoYouTip

Webpages Razor

# ASP.NET Web Pages - Adding Razor Code * * * In this tutorial, we will use the Razor markup with C# and Visual Basic code. * * * ## What is Razor? * Razor is a markup syntax for adding server-based code to web pages * Razor has the power of traditional ASP.NET markup, but is easier to use and easier to learn * Razor is a server-side markup syntax, much like ASP and PHP * Razor supports C# and Visual Basic programming languages * * * ## Adding Razor Code Remember the webpage from the example in the previous chapter: Web Pages Demo

Hello Web Pages

Now add some Razor code to the example: ## Example Web Pages Demo

Hello Web Pages

The time is @DateTime.Now

[Run Example Β»](#) The page above contains a line of Razor code marked by the `@` sign. The Razor code does all the work on the server, and produces the result displayed. (You can specify formatting options, otherwise the default will be used.) * * * ## Main Razor C# Syntax Rules * Razor code blocks are enclosed in `@{ ... }` * Inline expressions (variables and functions) start with `@` * Code statements end with semicolons * Variables are declared with the `var` keyword * Strings are enclosed with quotation marks * C# code is case sensitive * C# files have the extension `.cshtml` ## C# Example @{ var myMessage = "Hello World"; }

The value of myMessage is: @myMessage

@{ var greeting = "Welcome to our site!"; var weekDay = DateTime.Now.DayOfWeek; var greetingMessage = greeting + " Today is: " + weekDay; }

The greeting is: @greetingMessage

[Run Example Β»](#) * * * ## Main Razor VB Syntax Rules * Razor code blocks are enclosed in `@Code ... End Code` * Inline expressions (variables and functions) start with `@` * Variables are declared with the `Dim` keyword * Strings are enclosed with quotation marks * VB code is not case sensitive * VB files have the extension `.vbhtml` ## Example @Code dim myMessage = "Hello World" End Code

The value of myMessage is: @myMessage

@Code dim greeting = "Welcome to our site!" dim weekDay = DateTime.Now.DayOfWeek dim greetingMessage = greeting & " Today is: " & weekDay End Code

The greeting is: @greetingMessage

[Run Example Β»](#) * * * ## More About C# and Visual Basic If you want to learn more about Razor, C#, and Visual Basic programming languages, please see the (#) of this tutorial.
← Angularjs ScopesAngularjs Model β†’