Created
October 26, 2019 00:37
-
-
Save kironroy/638eaccf3f11caf4a64f3790b594242b to your computer and use it in GitHub Desktop.
Razor Syntax practice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@* https://stackoverflow.com/questions/6286868/convert-month-int-to-month-name/6286910 *@ | |
@* https://blogs.msdn.microsoft.com/timlee/2010/07/30/using-functions-in-an-asp-net-page-with-razor-syntax/ *@ | |
@{ | |
ViewData["Title"] = "RazorPractice"; | |
} | |
<h1>Razor Practice</h1> | |
@{ | |
for (int i = 1; i < 6; i++) | |
{ | |
<h3>@i Learning Razor Syntax</h3> | |
} | |
} | |
<hr /> | |
@* single line statements *@ | |
@{ | |
string message = @" ""I am a single line statement"" "; | |
} | |
<h4>The message is: @message</h4> | |
<hr /> | |
@* code block *@ | |
@{ | |
int numberOne = 11; | |
int numberTwo = 3; | |
var result = numberOne + numberTwo; | |
} | |
<h4>Sum of @numberOne + @numberTwo = @result</h4> | |
<hr /> | |
@* if/else *@ | |
@{ | |
string messageTwo; | |
int six = 6; | |
if (six < 6) | |
{ | |
messageTwo = "It is less than 6"; | |
} | |
else if (six > 6) | |
{ | |
messageTwo = "It is greater than 6"; | |
} | |
else | |
{ | |
messageTwo = "It is equal to 6"; | |
} | |
<h4>@messageTwo</h4> | |
} | |
<hr /> | |
@* combine html and razor *@ | |
@{ | |
string name = "Zelda"; | |
string role = "Princess"; | |
} | |
<h4>Hello @name. Your role is @role of Hyrule.</h4> | |
<hr /> | |
@* datetime *@ | |
@{ | |
string year; | |
string day; | |
string month; | |
string time; | |
year = DateTime.Now.Year.ToString(); | |
day = DateTime.Now.DayOfWeek.ToString(); | |
month = DateTime.Now.Month.ToString(); | |
time = DateTime.Now.ToShortTimeString(); | |
} | |
<h6> Current Time: @time</h6> | |
<h6> Current Day: @day</h6> | |
<h6> Current Month: @month</h6> | |
<h6> Current Year: @year</h6> | |
<hr /> | |
@* converting integer *@ | |
@{ | |
var numberToConvert = "626"; | |
int numberConverted; | |
if (int.TryParse(numberToConvert, out numberConverted)) | |
{ | |
<h5>@numberConverted is integer value</h5> | |
} | |
else | |
{ | |
<h5>@numberConverted is not integer</h5> | |
} | |
} | |
<hr /> | |
@{ | |
var b = "false"; | |
bool bln; | |
if (Boolean.TryParse(b, out bln)) | |
{ | |
<h5>Boolean is: @bln</h5> | |
} | |
} | |
<hr /> | |
@* operators *@ | |
@{ | |
int numberOneOper = 81; | |
int numberTwoOper = 9; | |
int plus = numberOneOper + numberTwoOper; | |
int minus = numberOneOper - numberTwoOper; | |
int multiply = numberOneOper * numberTwoOper; | |
int divide = numberOneOper / numberTwoOper; | |
} | |
<h6>Addition: @numberOneOper + @numberTwoOper = @plus</h6> | |
<h6>Subtraction: @numberOneOper - @numberTwoOper = @minus</h6> | |
<h6>Multiply: @numberOneOper * @numberTwoOper = @multiply</h6> | |
<h6>Division: @numberOneOper / @numberTwoOper = @divide</h6> | |
<hr /> | |
@* nested for loop*@ | |
@{ | |
int[] numArrayOne = { 12, 52, 66, 92, 28 }; | |
int[] numArrayTwo = { 73, 12, 98, 23, 52 }; | |
for (int i = 0; i < 5; i++) | |
{ | |
for (int j = 0; j < 5; j++) | |
{ | |
if (numArrayOne[i] == numArrayTwo[j]) | |
{ | |
<h5>Match found: @numArrayOne[i]</h5> | |
} | |
} | |
} | |
} | |
<hr /> | |
@* foreach list *@ | |
@{ | |
List<string> firstNames = new List<string>(); | |
firstNames.Add("Sita"); | |
firstNames.Add("Gita"); | |
firstNames.Add("Mita"); | |
foreach (string firstName in firstNames) | |
{ | |
<h3><i>My name is: @firstName</i></h3> | |
} | |
} | |
<hr /> | |
@* != *@ | |
@{ | |
int numberSix = 6; | |
int numberTen = 10; | |
if (numberSix != numberTen) | |
{ | |
<h6>Returns True</h6> | |
} | |
} | |
<hr /> | |
@* string concat *@ | |
@{ | |
string concatThis = "This is a Razor tutorial"; | |
string withThis = ", it's simple!"; | |
string concatFinal = $"{concatThis}{withThis}"; | |
<h4>@concatFinal</h4> | |
} | |
<hr /> | |
@* increment/decrement *@ | |
@{ | |
int incrementThis = 8; | |
<h5>Increment of @incrementThis is @{incrementThis++;}: @incrementThis </h5> | |
<h5>Decrement of @incrementThis is @{incrementThis--;}: @incrementThis </h5> | |
<h5>Increment of @incrementThis by 2 is @{incrementThis += 2;}: @incrementThis </h5> | |
<h5>Decrement of @incrementThis by 2 is @{incrementThis -= 2;}: @incrementThis </h5> | |
} | |
<hr /> | |
@* array access by index *@ | |
@{ | |
int[] arrayAccessExample = { 12, 3, 5, 6 }; | |
<h3>The index at slot 2 is: @arrayAccessExample[2]</h3> | |
} | |
<hr /> | |
@* logical operators *@ | |
@{ | |
string userName = "Kelly"; | |
string password = "Kells*Bells!"; | |
if (userName == "Kelly" && password == "Kells*Bells!") | |
{ | |
<h3>Login Successful</h3> | |
} | |
else if (userName == "Kelly" || password == "1234") | |
{ | |
<h3>Hello Kelly, you have entered the wrong password.</h3> | |
} | |
else | |
{ | |
<h3>Unauthorized password</h3> | |
} | |
} | |
<hr /> | |
@* if/else *@ | |
@{ | |
if (DateTime.Now.DayOfWeek.ToString() == "Sunday") | |
{ | |
<h3>It's Sunday</h3> | |
} | |
else if (DateTime.Now.DayOfWeek.ToString() == "Monday") | |
{ | |
<h3>Monday Blues</h3> | |
} | |
else | |
{ | |
<h3>Today is: @DateTime.Now.DayOfWeek</h3> | |
} | |
} | |
<hr /> | |
@* while loop *@ | |
@{ | |
int whileLoop = 0; | |
while (whileLoop < 6) | |
{ | |
<h4>This while loop prints 6 times</h4> | |
whileLoop++; | |
} | |
} | |
<hr /> | |
@* array *@ | |
@{ | |
<h3>Single dimensional array</h3> | |
int[] numbers = { 1, 3, 5, 7, 11, 13, 17, 19 }; | |
foreach (int number in numbers) | |
{ | |
<span>@number, </span> | |
} | |
// multi dim array | |
<h3>Multi dimensional array</h3> | |
string[,] letter = new string[3, 2] { { "a", "b" }, { "c", "d" }, { "e", "f" } }; | |
for (int i = 0; i < 3; i++) | |
{ | |
for (int j = 0; j < 2; j++) | |
{ | |
<span>@letter[i, j]</span> | |
} | |
} | |
<br /> | |
} | |
<hr /> | |
@* dictonaries *@ | |
@{ | |
var peopleDict = new Dictionary<string, int>(); | |
peopleDict.Add("Sita", 88); | |
peopleDict.Add("Gita", 78); | |
peopleDict.Add("Mita", 68); | |
} | |
<h3>Results of the student's grade are</h3> | |
<h4>Sita of the student's grade are: @peopleDict["Sita"] </h4> | |
<h4>Gita of the student's grade are: @peopleDict["Gita"] </h4> | |
<h4>Mita of the student's grade are: @peopleDict["Mita"] </h4> | |
<hr /> | |
@* --------------------------- methods --------------------------- *@ | |
@functions{ | |
public string SayHello(string firstName, string lastName) | |
{ | |
return $"Hello my name is {firstName} {lastName}"; | |
} | |
} | |
<h3>@SayHello("Grace", "Kelly")</h3> | |
<hr /> | |
@functions{ | |
public string DateFormated() | |
{ | |
string year = DateTime.Now.Year.ToString(); | |
string day = DateTime.Now.DayOfWeek.ToString(); | |
string time = DateTime.Now.ToShortTimeString(); | |
DateTime month = DateTime.Now; | |
string monthProper = month.ToString("MMMM"); | |
return $"The date is: {monthProper}, {day}, {year}.\n The time is {time}"; | |
} | |
} | |
<h3>@DateFormated()</h3> | |
<hr /> | |
@*error handling*@ | |
@functions{ | |
public int Divide(int x, int y) | |
{ | |
string cannotDivideMessage; | |
try | |
{ | |
return x / y; | |
} | |
catch (DivideByZeroException e) | |
{ | |
cannotDivideMessage = e.ToString(); | |
WriteLiteral("<br /> Cannot divide by zero <br /> <br/>"); | |
WriteLiteral($"Additional information: <br/> <br/> {cannotDivideMessage}"); | |
return 0; | |
} | |
} | |
} | |
<h6>Division of 10 / 5 is @Divide(10,5)</h6> | |
<h6>Add of 12 + 0 is @Divide(12,0)</h6> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment