TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

ASP.Net Razor Code Blocks

ASP.Net Razor Code Blocks with asp.net tutorial, asp.net introduction, features, project, example, server controls, labels, textbox, button, hyperlink, radiobutton, calender, checkbox, fileupload, events handling, authentication, webforms model binding, html server control, compare validdator, range validator, validation summary, mvc introduction, mvc project, view, validation, entity framework, authentication etc.

<< Back to ASP

ASP.NET Razor Code Blocks

Code block is used to enclose C# code statements. It starts with @ (at) character and is enclosed by {} (curly braces). Unlike expressions, C# code inside code blocks is not rendered. The default language in a code block is C#, but we can transit back to HTML. HTML within a code block will be rendered as HTML.


Example

// Index.cshtml

@{
    Layout = null;
    var name = "John";
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <h2>My name is: @name </h2>
</body>
</html>

It produces the following output.

ASP Razor code blocks 1

Implicit transitions

C# is default language in Razor code block. HTML written within code block is rendered as HTML, this is called implicit transition. Razor code blocks implicit transitions HTML code and render to the view page.

In the following code, HTML is written and it executes without error.

// Index.cshtml

@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
@{
    var name = "JavaTpoint";
    <h4>Welcome to the @name </h4>
}
</body>
</html>

It produces the following output.

ASP Razor code blocks 2

Explicit delimited transition

Sometimes, when we define a sub-section of a code block that should render HTML, surround the characters to be rendered with the Razor <text> tag.

It is mandate to use <text> tag. Otherwise, it throws a compile time error. See the following code.

// Index.cshtml

@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
@for (var i = 0; i < 5; i++)
{
    <text>i= @i </text> <br/>
}
</body>
</html>

It produces the following output.

ASP Razor code blocks 3




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf