Home » ASP.Net Razor Control Structures

ASP.Net Razor Control Structures

by Online Tutorials Library

ASP.NET Razor Control Structures

Control structures are control statements that are used to control program flow. C# programming language uses if, else, if else, switch, for, foreach, while to perform conditional logic in the application.

Razor engine supports all these controls in the view file. Let’s see some examples that implements control structure using razor syntax.


@if

// RazorControlStructure.cshtml

Output:

It produces the following output.

ASP Razor control structure 1


Else and Else If

The @ (at) symbol is not require in else and else if statements.

// RazorControlStructure.cshtml

Output:

ASP Razor control structure 2


@switch Example

// RazorControlStructure.cshtml

Output:

ASP Razor control structure 3


@for

// RazorControlStructure.cshtml

Output:

It produces the following output.

ASP Razor control structure 4

You may also like