TheDeveloperBlog.com

Home | Contact Us

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

ASP.Net Razor Code Expressions

ASP.Net Razor Code Expressions 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 Expressions

Razor syntax is widely used with C# programming language. To write C# code into a view use @ (at) sign to start Razor syntax. We can use it to write single line expression or multiline code block. Let's see how we can use C# code in the view page.

The following example demonstrate code expression.

// Index.cshtml

@{
    Layout = null;
    var coursename = "Java Collection";
}
<!DOCTYPE html> 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <h2>I want to learn @coursename </h2>
</body>
</html>

Produce the following output.

Output:

ASP Razor code expression 1

Implicit Razor Expressions

Implicit Razor expression starts with @ (at) character followed by C# code. The following example demonstrates about implicit expressions.

// Index.cshtml

@{
    Layout = null;
}
<!DOCTYPE html>  
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <p>Current Time is: @DateTime.Now.ToString("T")</p>
</body>
</html>

It produces the following output.

Output:

ASP Razor code expression 2

Explicit Razor Expressions

Explicit Razor expression consists of @ (at) character with balanced parenthesis. In the following example, expression is enclosed with parenthesis to execute safely. It will throw an error if it is not enclosed with parenthesis.

We can use explicit expression to concatenate text with an expression.

// Index.cshtml

@{
    Layout = null;
}
<!DOCTYPE html>  
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <p>2 + 5 = @(2+5)</p>
</body>
</html>

It produces the following output.

Output:

ASP Razor code expression 3

Razor Expression Encoding

Razor provides expression encoding to avoid malicious code and security risks. In case, if user enters a malicious script as input, razor engine encode the script and render as HTML output.

Here, we are not using razor syntax in view page.

// Index.cshtml

@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    Html.Raw("<script>alert('System Failure!')</script>")
</body>
</html>

It produces the following output.

Output:

ASP Razor code expression 4

In the following example, we are encoding JavaScript script.

// Index.cshtml

@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    @("<script>alert('this is alert box')</script>")
</body>
</html>

Now, it produces the following output.

Output:

ASP Razor code expression 5

This time razor engine encodes the script and return as a simple HTML string.






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