In this tutorial, we will go over the Razor Syntax used in Visual Basic and how it can help you write code faster. Razor is meant to allow embedding server-based code into web pages in a simpler syntax that is similar to PHP and classic ASP. The nice part is that you can use Razor in both VB and C# (if you now the right syntax that is).

Razor Syntax Rules

Let’s look at some of guidelines to follow when using Razor inside your Visual Basic project

Razor Usage 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

As you can see, there are a couple of things to keep in mind when creating and developing your VB Razor site. But if we break it down and compare it to other languages we are familiar with, we notice that the only real difference is the @Code opening tag and that all our variables start with Dim instead of the usual var indicator. Let’s look at a few lines of code that we created that will sum up this guideline.

In this demonstration, it is easy to see how we can use Razor coding seamlessly within our HTML to create dynamic server data. By using the @Code/End Code tag, we are able to create an area to store all variables to use later. We use objects such as DateTime.Now.DayOfWeek to retrieve up to date data from the server and display it within our project.

Razor VB Logic Conditions

You may be familiar with logic whether using PHP or Javascript. Similar to these scripting languages, the If/Else/ElseIf statements are used to give the computer a set of guidelines and tell it what to do.

Razor Logic Conditions Rules

  • The if statement starts a code block
  • the condition is written between if and then
  • the code between if…then and end if is executed if the test is true

With Razor though, our syntax is a little different by removing Curly Brackets{} and starting our lines of code with @. Let’s look at an If/Else/ElseIf statement that covers all three logical statements.

If you only need the If statement, simply remove the ElseIf and Else and leave the EndIf closing tag. One interesting feature of Razor’s Conditionals is the use of the Select Conditions. This is another way of dictating logic from data you pull from the server. Let’s take a look

In this bit of code, We are retrieving the day of the week from the server and asking it to perform some logic based on what day of the week it is. The computer will determine what message to display depending on what the server provides, thus giving us different “Cases” to choose from. A very helpful feature.

Razor VB Loops and Arrays

Our last section we will go over is about Loops and Arrays. Every coding language needs a loop function. Without it, you would be wasting endless amounts of time repeating yourself over and over and retyping code. Thankfully Razor makes it very easy to create loops and arrays within Visual Basic projects. Let’s look into how to create loops.

Razor VB Loops

The syntax is similar to a standard Loop. With Razor, writing loops is faster and easier than before. Here is an example.

No longer do you have to write a long complex loop with a variable and comparison. You can simply state your variable’s (in this case i) value and the integer you want the loop to stop at. Short, Simple and effective.

Razor VB For Each Loops

The For Each loop gets a little more complex. Check out the example below.

This For Each loop is telling the computer to find a list of Server Variables, assign each one to x and then list them (with @x), Continue to the Next one until there are no more. Essentially listing all Server Variables to the document.

Razor VB For While Loops

The While loop will run a function similar to the For Loop, But will perform an action “While” the Loop is running.

Razor VB Arrays

Now the Arrays in Razor VB are set up differently than what you would expect. The array is setup in a Dim variable and labeled as “Strings()”. Lets take a look.

Our first set within the @Code is all of our variables and operations that will calculate and be stored within a variable. Our first line holds the Array that contains the meat of the code. Later in our code, we call out to our variables with the Razor @ symbol which will run through all are variables and Arrays to get the answer.

Wrapping it Up

If you are looking for an easier way to write Visual Basic, Razor syntax is the answer. It helps minify the amount of code you write and does a great job in using words instead of symbols (like ; : {}[]) to illustrate logic or code. Once you get used to the syntax, try and write your code in the C# language to get more practice.