{"id":26863090,"url":"https://github.com/jwill9999/asp.net-razor-cheatsheet","last_synced_at":"2025-07-22T15:08:43.804Z","repository":{"id":127600670,"uuid":"160362420","full_name":"jwill9999/ASP.NET-Razor-Cheatsheet","owner":"jwill9999","description":"ASP.NET Razor Cheatsheet","archived":false,"fork":false,"pushed_at":"2018-12-04T13:34:24.000Z","size":5,"stargazers_count":2,"open_issues_count":0,"forks_count":4,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-05-06T20:35:58.160Z","etag":null,"topics":["asp-net","asp-net-core","asp-net-core-mvc","aspnet-core","razor-pages"],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jwill9999.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2018-12-04T13:33:23.000Z","updated_at":"2022-12-31T06:30:58.000Z","dependencies_parsed_at":null,"dependency_job_id":"37d52c1e-edad-4d0a-b8ca-38eef046ce4c","html_url":"https://github.com/jwill9999/ASP.NET-Razor-Cheatsheet","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/jwill9999/ASP.NET-Razor-Cheatsheet","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwill9999%2FASP.NET-Razor-Cheatsheet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwill9999%2FASP.NET-Razor-Cheatsheet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwill9999%2FASP.NET-Razor-Cheatsheet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwill9999%2FASP.NET-Razor-Cheatsheet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jwill9999","download_url":"https://codeload.github.com/jwill9999/ASP.NET-Razor-Cheatsheet/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwill9999%2FASP.NET-Razor-Cheatsheet/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266516304,"owners_count":23941392,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-22T02:00:09.085Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asp-net","asp-net-core","asp-net-core-mvc","aspnet-core","razor-pages"],"created_at":"2025-03-31T02:39:44.303Z","updated_at":"2025-07-22T15:08:43.793Z","avatar_url":"https://github.com/jwill9999.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# ASP.NET Razor - C# Code Syntax - [PDF Download Cheatsheet](https://1drv.ms/b/s!Ai0GNI50Q5GAgdQ0B2b3gDPlXcxM_w)\n\n\u003cdiv align=\"center\"\u003e\n\u003cimg src=\"https://vietsang.club/wp-content/uploads/2018/10/ASP.Net8_-1.png\" width=100%/\u003e\n\u003c/div\u003e\n\n\u003cbr\u003e\n\u003cbr\u003e\n\u003cbr\u003e\n\n# Index\u003cbr\u003e\n\n[Razor Comments](#Razor-Comments)\u003cbr\u003e\n[Inline expression](#Inline-expression)\u003cbr\u003e\n[Multi-statement block](#Multi-statement-block)\u003cbr\u003e\n[Store String](#Store-Date)\u003cbr\u003e\n[Store Date](#Tag-Helpers)\u003cbr\u003e\n[Store Date](#Tag-Helpers)\u003cbr\u003e\n[Variables](#Variables)\u003cbr\u003e\n[Convert Data Types ](#Convert-Data-Types )\u003cbr\u003e\n[Loops](#Loops)\u003cbr\u003e\n[Arrays](#Arrays)\u003cbr\u003e\n[Conditionals](#Conditionals)\u003cbr\u003e\n[Using](#Using)\u003cbr\u003e\n[Models View](#Models-View)\u003cbr\u003e\n[Dependency Injection](#Dependency-Injection)\u003cbr\u003e\n[Add Functions](#Add-Functions)\u003cbr\u003e\n[Create Templates ](#Create-Templates )\u003cbr\u003e\n[Conditional Attributes](#Conditional-Attributes)\u003cbr\u003e\n[Forms](#Forms)\u003cbr\u003e\n[Add Partials](#Add-Partials)\u003cbr\u003e\n[Add link to a page](#Add-link-to-a-page)\u003cbr\u003e\n[ Loop through a list and output](#Loop-through-a-list-and-output)\n\n\n\n\n### Razor is a markup syntax that lets you embed server-based code (Visual Basic and C#) into web pages\n\n## Razor Comments\n\n```c#\n@*  A one-line code comment. *@\n\n@*\n    This is a multiline code comment.\n    It can continue for any number of lines.\n*@\n```\n```c#\n@{\n    @* This is a comment. *@\n    var theVar = 17;\n}\n```\n\n\u003cbr\u003e\n\n[back to top](#Index)\u003cbr\u003e\n\n\u003cbr\u003e\n\n## Single statement block\n\n```c#\n@{ var myMessage = \"Hello World\"; }\n```\n\n## Inline expression\n\n```c#\n@{ var myMessage = \"Hello World\"; }\n```\n## Multi statement block\n\n```c#\n@{\nvar greeting = \"Welcome to our site!\";\nvar weekDay = DateTime.Now.DayOfWeek;\nvar greetingMessage = greeting + \" Here in Huston it is: \" + weekDay;\n}\n\u003cp\u003eThe greeting is: @greetingMessage\u003c/p\u003e }\n```\n\n\u003cbr\u003e\n\n[back to top](#Index)\u003cbr\u003e\n\n\u003cbr\u003e\n\n## Store String \n\n```c#\n/* A string is a sequence of characters that are treated as text. To specify a string, you enclose it in double quotation marks:*/\n\n@{ var welcomeMessage = \"Welcome, new members!\"; }\n\u003cp\u003e@welcomeMessage\u003c/p\u003e\n```\n## Store Date \n\n```c#\n@{ var year = DateTime.Now.Year; }\n```\n\n\u003cbr\u003e\n\n[back to top](#Index)\u003cbr\u003e\n\n\u003cbr\u003e\n\n## Read User Input\n\n```c#\n@{\nvar totalMessage = \"\";\nif(IsPost)\n    {\n    var num1 = Request[\"text1\"];\n    var num2 = Request[\"text2\"];\n    var total = num1.AsInt() + num2.AsInt();\n    totalMessage = \"Total = \" + total;\n}\n}\n\n```\n\n\u003cbr\u003e\n\n[back to top](#Index)\u003cbr\u003e\n\n\u003cbr\u003e\n\n## Variables\n\n```c#\n@{\n    // Assigning a string to a variable.\n    var greeting = \"Welcome!\";\n\n    // Assigning a number to a variable.\n    var theCount = 3;\n\n    // Assigning an expression to a variable.\n    var monthlyTotal = theCount + 5;\n\n    // Assigning a date value to a variable.\n    var today = DateTime.Today;\n\n    // Assigning the current page's URL to a variable.\n    var myPath = this.Request.Url;\n\n    // Declaring variables using explicit data types.\n    string name = \"Joe\";\n    int count = 5;\n    DateTime tomorrow = DateTime.Now.AddDays(1);\n}\n\n```\n\n### Display Variables\n\n```c#\n@{\n    // Embedding the value of a variable into HTML markup.\n    \u003cp\u003e@greeting, friends!\u003c/p\u003e\n\n    // Using variables as part of an inline expression.\n    \u003cp\u003eThe predicted annual total is: @( monthlyTotal * 12)\u003c/p\u003e\n\n    // Displaying the page URL with a variable.\n    \u003cp\u003eThe URL to this page is: @myPath\u003c/p\u003e\n}\n\n```\n\n\n\u003cbr\u003e\n\n[back to top](#Index)\u003cbr\u003e\n\n\u003cbr\u003e\n\n## Convert Data Types \n\n\n| Method     | Description          |Examples  |\n| ------------- |:-------------:| -----:|\n|AsInt(),\u003cbr\u003e IsInt() |   Converts a string to an integer.   | ``` if (myString.IsInt())```\u003cbr\u003e  ```{myInt=myString.AsInt();```|   \n|AsFloat(), IsFloat()| Converts a string to a floating-point number.|```if (myString.IsFloat())```\u003cbr\u003e```{myFloat=myString.AsFloat();}```|\n|AsDecimal(), IsDecimal()| Converts a string to a decimal number..|```if (myString.IsDecimal())```\u003cbr\u003e```{myDec=myString.AsDecimal();}```|\n|AsDateTime(), IsDateTime()|Converts a string to an ASP.NET DateTime type.| ```myString=\"10/10/2012\";```\u003cbr\u003e``` myDate=myString.AsDateTime();```|\n|AsBool(),\u003cbr\u003e IsBool()|Converts a string to a Boolean..| ```myString=\"True\";```\u003cbr\u003e ```myBool=myString.AsBool();```|\n|ToString()|Converts any data type to a string.| ```myInt=1234;```\u003cbr\u003e ```myString=myInt.ToString();```|\n\n### Coverting Data Types example\n\n```c#\n@{\n    var total = 0;\n\n    if(IsPost) {\n        // Retrieve the numbers that the user entered.\n        var num1 = Request[\"text1\"];\n        var num2 = Request[\"text2\"];\n        // Convert the entered strings into integers numbers and add.\n        total = num1.AsInt() + num2.AsInt();\n    }\n}\n\n```\n\n\u003cbr\u003e\n\n[back to top](#Index)\u003cbr\u003e\n\n\u003cbr\u003e\n\n# Loops\n\n## Standard Loop\n\n```c#\n@for (var i = 0; i \u003c people.Length; i++)\n{\n    var person = people[i];\n    \u003ctext\u003eName: @person.Name\u003c/text\u003e\n}\n```\n\n  \n\n## ForEach Loops \n\n```c#\n\u003cul\u003e\n@foreach (var myItem in Request.ServerVariables)\n{\n    \u003cli\u003e@myItem\u003c/li\u003e\n}\n\u003c/ul\u003e\n```\n## While Loops \n\n```c#\n@{\n    var countNum = 0;\n    while (countNum \u003c 50)\n    {\n        countNum += 1;\n        \u003cp\u003eLine #@countNum: \u003c/p\u003e\n    }\n}\n```\n\n\n\u003cbr\u003e\n\n[back to top](#Index)\u003cbr\u003e\n\n\u003cbr\u003e\n\n## Arrays \n\n```c#\n@{\nstring[] members = {\"Jani\", \"Hege\", \"Kai\", \"Jim\"};\nint i = Array.IndexOf(members, \"Kai\")+1;\nint len = members.Length;\nstring x = members[2-1];\n}\n\u003chtml\u003e\n\u003cbody\u003e\n\u003ch3\u003eMembers\u003c/h3\u003e\n@foreach (var person in members)\n{\n\u003cp\u003e@person\u003c/p\u003e\n}\n\u003cp\u003eThe number of names in Members are @len\u003c/p\u003e\n\u003cp\u003eThe person at position 2 is @x\u003c/p\u003e\n\u003cp\u003eKai is now in position @i\u003c/p\u003e\n\u003c/body\u003e\n\u003c/html\u003e\n```\n\n\n\u003cbr\u003e\n\n[back to top](#Index)\u003cbr\u003e\n\n\u003cbr\u003e\n\n\n# Conditionals\n\n## If \n\n```c#\n@{\n  var showToday = true;\n  if(showToday)\n  {\n    @DateTime.Today;\n  }\n}\n```\n## If Else \n\n```c#\n@{\n  var showToday = false;\n  if(showToday)\n  {\n    @DateTime.Today;\n  }\n  else\n  {\n    \u003ctext\u003eSorry!\u003c/text\u003e\n  }\n}\n```\n## Else If \n\n```c#\n@{\n    var theBalance = 4.99;\n    if(theBalance == 0)\n    {\n        \u003cp\u003eYou have a zero balance.\u003c/p\u003e\n    }\n    else if (theBalance  \u003e 0 \u0026\u0026 theBalance \u003c= 5)\n    {\n        \u003cp\u003eYour balance of $@theBalance is very low.\u003c/p\u003e\n    }\n    else\n    {\n        \u003cp\u003eYour balance is: $@theBalance\u003c/p\u003e\n    }\n}\n```\n## Switch Statement \n\n```c#\n@{\n    var weekday = \"Wednesday\";\n    var greeting = \"\";\n\n    switch(weekday)\n    {\n        case \"Monday\":\n            greeting = \"Ok, it's a marvelous Monday\";\n            break;\n        case \"Tuesday\":\n            greeting = \"It's a tremendous Tuesday\";\n            break;\n        case \"Wednesday\":\n            greeting = \"Wild Wednesday is here!\";\n            break;\n        default:\n            greeting = \"It's some other day, oh well.\";\n            break;\n    }\n\n    \u003cp\u003eSince it is @weekday, the message for today is: @greeting\u003c/p\u003e\n}\n```\n## Try Catch Finally\n\n```c#\n@try\n{\n    throw new InvalidOperationException(\"You did something invalid.\");\n}\ncatch (Exception ex)\n{\n    \u003cp\u003eThe exception message: @ex.Message\u003c/p\u003e\n}\nfinally\n{\n    \u003cp\u003eThe finally statement.\u003c/p\u003e\n}\n\n```\n\n\n\u003cbr\u003e\n\n[back to top](#Index)\u003cbr\u003e\n\n\u003cbr\u003e\n\n\n## Using \n\n```c#\n\n/* The @using directive adds the C# using directive to the generated view:*/\n\n@using System.IO\n@{\n    var dir = Directory.GetCurrentDirectory();\n}\n\u003cp\u003e@dir\u003c/p\u003e\n\n```\n\n\u003cbr\u003e\n\n[back to top](#Index)\u003cbr\u003e\n\n\u003cbr\u003e\n\n\n## Models View\n\n```c#\n// The @model directive specifies the type of the model passed to a view:\n\n@model TypeNameOfModel\n\n```\n\n## Access Model \n\n```html\n\u003cdiv\u003eThe Login Email: @Model.Email\u003c/div\u003e\n```\n\n## Dependency Injection\n\n```c#\n@inject +ServiceName\n```\n\n\u003cbr\u003e\n\n[back to top](#Index)\u003cbr\u003e\n\n\u003cbr\u003e\n\n## Add Functions\n\n```html\n@functions {\n    public string GetHello()\n    {\n        return \"Hello\";\n    }\n}\n\n\u003cdiv\u003eFrom method: @GetHello()\u003c/div\u003e\n\n```\n\n\u003cbr\u003e\n\n[back to top](#Index)\u003cbr\u003e\n\n\u003cbr\u003e\n\n## Create Templates \n\nCreate a class\n\n```c#\npublic class Pet\n{\n    public string Name { get; set; }\n}\n```\n\ncreate a .```cshtml``` page\n\n```html\n@{\n    Func\u003cdynamic, object\u003e petTemplate = @\u003cp\u003eYou have a pet named @item.Name.\u003c/p\u003e;\n\n    var pets = new List\u003cPet\u003e\n    {\n        new Pet { Name = \"Rin Tin Tin\" },\n        new Pet { Name = \"Mr. Bigglesworth\" },\n        new Pet { Name = \"K-9\" }\n    };\n\n     \u003c!-- The template is rendered with pets supplied by a foreach statement: --\u003e\n\n     @foreach (var pet in pets)\n{\n    @petTemplate2(pet)\n}\n}\n\n```\n\nRendered output\n\n```html\n\u003cp\u003eYou have a pet named \u003cstrong\u003eRin Tin Tin\u003c/strong\u003e.\u003c/p\u003e\n\u003cp\u003eYou have a pet named \u003cstrong\u003eMr. Bigglesworth\u003c/strong\u003e.\u003c/p\u003e\n\u003cp\u003eYou have a pet named \u003cstrong\u003eK-9\u003c/strong\u003e.\u003c/p\u003e\n```\n\n\u003cbr\u003e\n\n[back to top](#Index)\u003cbr\u003e\n\n\u003cbr\u003e\n\n## Conditional Attributes\n\n```html\n@{\n    string divStyle = null;\n    if(Request.QueryString[\"style\"] != null)\n    {\n        divStyle = \"background-color: yellow;\";\n    }\n}\n\u003cdiv style=\"@divStyle\"\u003eHello, world!\u003c/div\u003e\n```\n\n\n\u003cbr\u003e\n\n[back to top](#Index)\u003cbr\u003e\n\n\u003cbr\u003e\n\n## Forms\n\n```html\n\u003cform method=\"post\"\u003e\n            \u003cdiv asp-validation-summary=\"ModelOnly\" class=\"text-danger\"\u003e\u003c/div\u003e\n            \u003cdiv class=\"form-group\"\u003e\n                \u003clabel asp-for=\"Movie.Title\" class=\"control-label\"\u003e\u003c/label\u003e\n                \u003cinput asp-for=\"Movie.Title\" class=\"form-control\" /\u003e\n                \u003cspan asp-validation-for=\"Movie.Title\" class=\"text-danger\"\u003e\u003c/span\u003e\n            \u003c/div\u003e\n            \u003cdiv class=\"form-group\"\u003e\n                \u003clabel asp-for=\"Movie.ReleaseDate\" class=\"control-label\"\u003e\u003c/label\u003e\n                \u003cinput asp-for=\"Movie.ReleaseDate\" class=\"form-control\" /\u003e\n                \u003cspan asp-validation-for=\"Movie.ReleaseDate\" class=\"text-danger\"\u003e\u003c/span\u003e\n            \u003c/div\u003e           \n            \u003cdiv class=\"form-group\"\u003e\n                \u003cinput type=\"submit\" value=\"Create\" class=\"btn btn-default\" /\u003e\n            \u003c/div\u003e\n\u003c/form\u003e\n\n```\n\n\u003cbr\u003e\n\n[back to top](#Index)\u003cbr\u003e\n\n\u003cbr\u003e\n\n\n## Add Partials \n\n```js\n@section Scripts {\n    @{await Html.RenderPartialAsync(\"_ValidationScriptsPartial\");}\n}\n```\n\n\u003cbr\u003e\n\n[back to top](#Index)\u003cbr\u003e\n\n\u003cbr\u003e\n\n## Add link to a page\n\n```html\n\u003cdiv\u003e\n    \u003ca asp-page=\"./Index\"\u003eBack to List\u003c/a\u003e\n\u003c/div\u003e\n```\n\n\u003cbr\u003e\n\n[back to top](#Index)\u003cbr\u003e\n\n\u003cbr\u003e\n\n## Loop through a list and output\n\n```html\n\u003ctbody\u003e\n        @foreach (var item in Model.Movie)\n        {\n            \u003ctr\u003e\n                \u003ctd\u003e\n                    @Html.DisplayFor(modelItem =\u003e item.Title)\n                \u003c/td\u003e\n                \u003ctd\u003e\n                    @Html.DisplayFor(modelItem =\u003e item.ReleaseDate)\n                \u003c/td\u003e\n                \u003ctd\u003e\n                    @Html.DisplayFor(modelItem =\u003e item.Genre)\n                \u003c/td\u003e\n                \u003ctd\u003e\n                    @Html.DisplayFor(modelItem =\u003e item.Price)\n                \u003c/td\u003e\n                \u003ctd\u003e\n                    \u003ca asp-page=\"./Edit\" asp-route-id=\"@item.ID\"\u003eEdit\u003c/a\u003e |\n                    \u003ca asp-page=\"./Details\" asp-route-id=\"@item.ID\"\u003eDetails\u003c/a\u003e |\n                    \u003ca asp-page=\"./Delete\" asp-route-id=\"@item.ID\"\u003eDelete\u003c/a\u003e\n                \u003c/td\u003e\n            \u003c/tr\u003e\n        }\n    \u003c/tbody\u003e\n\n```\n\n\n\u003cbr\u003e\n\n[back to top](#Index)\u003cbr\u003e\n\n\u003cbr\u003e\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjwill9999%2Fasp.net-razor-cheatsheet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjwill9999%2Fasp.net-razor-cheatsheet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjwill9999%2Fasp.net-razor-cheatsheet/lists"}