Ejemplos Razor


@page
@model RazorPruebas.IndexModel
@{
}
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
@* Single Line Statements *@
@{var message = "I am Single Line Statements";}
@* Inline Markup *@
<h5>The message is : @message</h5>

@* This is Code Block *@
@{
var num1 = 5;
var num2 = 10;
var result = num1 + num2;
}
@* Inline Markup *@
<h5>Sum of @num1 + @num2 = @result</h5>

@{
<h5>Url : @Request.Path</h5>
<h5>Host : @Request.Host</h5>

}

@{
int i = 5;
if (i < 5)
{
message = "It is less than 5";
}
else
{
message = "It is greater than 5";
}
}

@* I am comment and I will not be appear on the page *@
@*
I am multi line comment.
I will also not appear in web pages.
*@
<h5>@message</h5>

@{
string year, day, month, time;
year = DateTime.Now.Year.ToString();
day = DateTime.Now.DayOfWeek.ToString();
month = DateTime.Now.Month.ToString();
time = DateTime.Now.ToShortTimeString();
}

<h5>Current Year: @year</h5>
<h5>Current Day : @day</h5>
<h5>Current Month: @month</h5>
<h5>Current Time: @time</h5>

@{
int plus, minus, multi, divide;
num1 = 45;
num2 = 9;
plus = num1 + num2;
minus = num1 - num2;
multi = num1 * num2;
divide = num1 / num2;
}
<h5>Addition : @num1 + @num2 = @plus</h5>
<h5>Subtraction : @num1 - @num2 = @minus</h5>
<h5>Multiplication : @num1 * @num2 = @multi</h5>
<h5>Divide : @num1 / @num2 = @divide</h5>

@{
int[] anum1 = { 12, 52, 66, 92, 28 };
int[] anum2 = { 73, 12, 98, 23, 52 };

for (i = 0; i < 5; i++)
{
for (int loop = 0; loop < 5; loop++)
{
if (anum1[i] == anum2[loop])
{
<h5>Match Found : @anum1[i]</h5>
}
}
}
}

@{
int[] arr = { 1, 3, 5, 7, 11, 13, 17, 19 };
foreach (int x in arr)
{
<span>@x, </span>
}
}

@{
//Single Dimensional Array
<h3>Single Dimensional Array</h3>
int[] arr2 = { 1, 3, 5, 7, 11, 13, 17, 19 };
foreach (int x in arr2)
{
<span>@x, </span>
}

//Multi Dimensional Array
<h3>Multi Dimensional Array</h3>
string[,] letter = new string[3, 2] { { "a", "b" }, { "c", "d" }, { "e", "f" } };
for (i = 0; i < 3; i++)
{
for (int j = 0; j < 2; j++)
{
<span>@letter[i, j], </span>
}
<br />
}
}

@{
var result2 = new Dictionary<string, int>();
result2.Add("Jack", 75);
result2.Add("Steven", 80);
result2.Add("Clark", 95);
}
<h3>Results of the students are:</h3>
<h3>Jack : @result2["Jack"]</h3>
<h3>Steven : @result2["Steven"]</h3>
<h3>Clark : @result2["Clark"]</h3>
@functions{
public int sum(int x, int y)
{
return x + y;
}
public int dividir(int x, int y)
{
string errormessage;
try
{
return x / y;
}
catch (DivideByZeroException d)
{
errormessage = d.ToString();
WriteLiteral("<br />Cannot Divide by Zero Exception found <br /><br />");
WriteLiteral("Additional Info <br /><br />" + errormessage);
return 0;
}
}
}
<h3>Add of 5 + 6 is @sum(5, 6)</h3>
<h3>Add of 9 + 10 is @sum(9, 10)</h3>

<h3>Division of 10 / 5 is @dividir(10, 5)</h3>
<h3>Add of 12 + 0 is @dividir(12, 0)</h3>
</body>
</html>

Publicado por

Avatar del usuario

Juan Pablo Fuentes

Formador de programación y bases de datos