WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

ASP.NET Razor - C# and VB Code Syntax


Razor underpins both C# (C sharp) and VB (Visual Basic).


Main Razor Syntax Rules for C#

  • Razor code squares are encased in @{ ... }
  • Inline articulations (factors and capacities) begin with @
  • Code explanations end with semicolon
  • Variables are proclaimed with the var keyword
  • Strings are encased with citation marks
  • C# code is case sensitive
  • C# records have the augmentation .cshtml

C# Example

<!- - Single explanation square - >
@{ var myMessage = "Hello World"; }

<!- - Inline articulation or variable - >
<p>The estimation of myMessage is: @myMessage</p>

<!- - Multi-explanation square - >
@{
var welcoming = "Welcome to our site!";
var weekDay = DateTime.Now.DayOfWeek;
var greetingMessage = welcoming + " Here in Huston it is: " + weekDay;
}

<p>The welcoming is: @greetingMessage</p>

Main Razor Syntax Rules for VB

  • Razor code squares are encased in @Code ... End Code
  • Inline articulations (factors and capacities) begin with @
  • Variables are announced with the Dim keyword
  • Strings are encased with citation marks
  • VB code isn't case sensitive
  • VB documents have the expansion .vbhtml

Example

<!- - Single articulation square  - - > 
@Code diminish myMessage = "Hello World" End Code
 
<!- - Inline articulation or variable - > 
<p>The estimation of myMessage is: @myMessage</p> 
 
<!- - Multi-articulation square - > 
@Code
diminish welcoming = "Welcome to our site!" 
diminish weekDay = DateTime.Now.DayOfWeek 
diminish greetingMessage = welcoming & " Here in Huston it is: " & weekDay
End Code


<p>The welcoming is: @greetingMessage</p>

How Does it Work?

Razor is a straightforward programming sentence structure for inserting server code in site pages.

Razor sentence structure depends on the ASP.NET system, the piece of the Microsoft.NET Framework that is explicitly intended for making web applications.  

The Razor punctuation gives all of you the intensity of ASP.NET, yet is utilizing a streamlined punctuation that is simpler to learn in case you're an amateur, and makes you more profitable in case you're a specialist.

Razor website pages can be depicted as HTML pages with two sorts of substance: HTML substance and Razor code.

When the server peruses the page, it runs the Razor code first, before it sends the HTML page to the program. The code that is executed on the server can perform errands that is impossible in the program, for instance getting to a server database. Server code can make dynamic HTML content on the fly, before it is sent to the program. Seen from the program, the HTML produced by server code is the same than static HTML content.

ASP.NET website pages with Razor sentence structure have the extraordinary record augmentation cshtml (Razor utilizing C#) or vbhtml (Razor utilizing VB).


Working With Objects

Server coding regularly includes objects.

The "DateTime" object is a normal worked in ASP.NET object, however articles can likewise act naturally characterized, a website page, a content box, a document, a database record, etc.

Objects may have strategies they can perform. A database record may have a "Save" strategy, a picture article may have a "Rotate" technique, an email article may have a "Send" strategy, thus on.

Objects likewise have properties that depict their qualities. A database record might have a FirstName and a LastName property (among others).

The ASP.NET DateTime object has a Now property (composed as DateTime.Now), and the Now property has a Day property (composed as DateTime.Now.Day). The model underneath tells the best way to get to certain properties of the DateTime object:

Example

<table border="1">
<tr>
<th width="100px">Name</th>
<td width="100px">Value</td>
</tr>
<tr>
<td>Day</td><td>@DateTime.Now.Day</td>
</tr>
<tr>
<td>Hour</td><td>@DateTime.Now.Hour</td>
</tr>
<tr>
<td>Minute</td><td>@DateTime.Now.Minute</td>
</tr>
<tr>
<td>Second</td><td>@DateTime.Now.Second</td>
</tr>
</td>
</table>
Run precedent »

If and Else Conditions

An critical element of dynamic site pages is that you can figure out what to do dependent on conditions.

The regular approach to do this is with the if ... else statements:

Example

@{
var txt = "";
if(DateTime.Now.Hour > 12)
  {txt = "Good Evening";}
else
  {txt = "Good Morning";}
}
<html>
<body>
<p>The message is @txt</p>
</body>
</html>

Reading User Input

Another vital component of dynamic website pages is that you can peruse client input.

Input is perused by the Request[] capacity, and posting (input) is tried by the IsPost condition:

Example

@{
var totalMessage = "";
if(IsPost)
    {
    var num1 = Request["text1"];
    var num2 = Request["text2"];
    var absolute = num1.AsInt() + num2.AsInt();
    totalMessage = "Total = " + total;
    }
}

<html>
<body style="background-shading: beige; textual style family: Verdana, Arial;">
<form action="" method="post">
<p><label for="text1">First Number:</label><br>
<input type="text" name="text1"/></p>
<p><label for="text2">Second Number:</label><br>
<input type="text" name="text2"/></p>
<p><input type="submit" value=" Add "/></p>
</form>
<p>@totalMessage</p>
</body>
</html>
Run precedent »