TheDeveloperBlog.com

Home | Contact Us

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

ASP.Net Razor Tutorial

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

Razor is a standard markup syntax that allows us to embed server code into the web pages. It uses its own syntax and keywords to generate view.

If there is server code in the web page, server executes that code first then send response to the browser. It allows us to perform logical tasks in the view page. We can create expressions, loops and variables in the view page.

It has simplified syntax which is easy to learn and code. This file extension is .cshtml.

@ Character

Razor uses this character to transit from HTML to C#. When @ symbol is used with razor syntax, it transits into Razor specific markup, otherwise it transitions into plain C#. We used it, to start single line expression, single statement block or multi-statement block.

Razor Keywords

  • functions
  • inherits
  • model
  • section
  • helper (Not supported by ASP.NET Core.)

Example

Let's create a view that has Razor syntax. Right click on the Controller folder and select add->controller, it will prompt the following dialog.

ASP Razor 1

Provide a name to the controller.

ASP Razor 2

Click on add button, this will create a controller and adding the following code.

//StudentsController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace RazorViewExample.Controllers
{
    public class StudentsController : Controller
    {
        // GET: Students
        public ActionResult Index()
        {
            return View();
        }
    }
}

// Index.cshtml

@{
    Layout = null;
    var name = "Joseph";
}
<!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 to the browser.

ASP Razor 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