ASPCode.net logo
  • Home 
  • Blogs 
  • Tags 
Blog
  1. Home
  2. Articles
  3. Razor Syntax Intro

Razor Syntax Intro

Posted on March 6, 2025  (Last modified on May 26, 2025) • 2 min read • 404 words
C#
 
C#
 
Share via
ASPCode.net
Link copied to clipboard

Video is in Swedish

On this page
  • Razor Syntax Introduction
  • Basic Syntax
  • Code Blocks
  • Inline Code: Used for short snippets of C# code that can be executed within an HTML element.
  • Block Code: Used for longer pieces of C# code that need to be executed in a specific context.
  • Partial Code: Used for reusable code blocks that can be shared across multiple templates.
  • HTML Helpers
  • Html.ActionLink: Used to create links to actions in your application.
  • Html.BeginForm: Used to create forms that submit data to an action in your application.
  • Html.DisplayFor: Used to display the value of a model property.
  • Conclusion
  • Video
  • Sourcecode

Razor Syntax Introduction  

Razor is a powerful templating engine used in ASP.NET Core and ASP.NET MVC applications to separate presentation logic from application logic. It allows developers to create dynamic web pages with ease, making it an essential tool for building modern web applications.

In this article, we will introduce the basics of Razor syntax, covering its key features and components.

Basic Syntax  

Razor syntax is based on C# and uses a combination of HTML and C# code. The syntax is designed to be easy to read and write, making it accessible to developers with varying levels of experience.

A basic Razor template consists of two parts: the HTML part and the C# part. The HTML part is used to define the structure of the page, while the C# part is used to add dynamic content.

Here’s an example of a simple Razor template:

@{
    var name = "John Doe";
}

<html>
  <body>
    <h1>Welcome @name!</h1>
  </body>
</html>

In this example, the @{ ... } block is used to define a code block in C#. The var name = "John Doe"; statement declares a variable named name and assigns it the value "John Doe". The @name! syntax is used to inject the value of the name variable into the HTML.

Code Blocks  

Razor provides several types of code blocks, including:

  • Inline Code: Used for short snippets of C# code that can be executed within an HTML element.  

  • Block Code: Used for longer pieces of C# code that need to be executed in a specific context.  

  • Partial Code: Used for reusable code blocks that can be shared across multiple templates.  

HTML Helpers  

Razor provides several HTML helpers, including:

  • Html.ActionLink: Used to create links to actions in your application.  

  • Html.BeginForm: Used to create forms that submit data to an action in your application.  

  • Html.DisplayFor: Used to display the value of a model property.  

Conclusion  

In this introduction to Razor syntax, we have covered the basics of using Razor templates in ASP.NET Core and ASP.NET MVC applications. We have seen how to define code blocks, use HTML helpers, and inject dynamic content into our templates.

With this knowledge, you are now ready to start building your own Razor-based web applications. Whether you’re a seasoned developer or just starting out, Razor provides a powerful toolset for creating dynamic and engaging user interfaces.

Video  

Swedish

Sourcecode  

Sourcecode
 
 Bootstrap Styling i ASP.net
Java och S3 lagring i AWS 
On this page:
  • Razor Syntax Introduction
  • Basic Syntax
  • Code Blocks
  • Inline Code: Used for short snippets of C# code that can be executed within an HTML element.
  • Block Code: Used for longer pieces of C# code that need to be executed in a specific context.
  • Partial Code: Used for reusable code blocks that can be shared across multiple templates.
  • HTML Helpers
  • Html.ActionLink: Used to create links to actions in your application.
  • Html.BeginForm: Used to create forms that submit data to an action in your application.
  • Html.DisplayFor: Used to display the value of a model property.
  • Conclusion
  • Video
  • Sourcecode
Follow me

I code in Java, C#, Golang, SQL and more

     
© 2024 Systementor AB
ASPCode.net
Code copied to clipboard