Description #
Razor is a server-side templating syntax used in ASP.NET to embed C# code within HTML, enabling dynamic content rendering in web applications built with the .NET ecosystem.
History #
Razor was introduced by Microsoft in 2011 as part of ASP.NET Web Pages and later adopted in ASP.NET MVC and ASP.NET Core. It was designed to simplify templating with a more readable and compact syntax compared to traditional ASPX markup.
Hello World Code #
<h1>@message</h1>
With backend context in C#:
@{
var message = "Hello, World!";
}
How to Run #
Option 1: Online
Try Razor in .NET Fiddle:
https://dotnetfiddle.net/
Or Blazor WASM playgrounds like:
https://blazorrepl.telerik.com/
Option 2: Local
Use Visual Studio or Visual Studio Code with ASP.NET Core:
dotnet new mvc -n HelloRazor
cd HelloRazor
dotnet run
Edit views in Views/Home/Index.cshtml
.
Key Concepts #
- Uses
@
to switch between HTML and C# .cshtml
is the standard Razor file extension- Supports loops, conditionals, and functions
- Deeply integrated into ASP.NET MVC and Razor Pages
- View components and partial views supported
- Can output raw or encoded content
- Also used in Blazor for building interactive UIs
- Encourages separation of logic and markup
- Supports layout pages and section overrides
- Precompiled for performance in production
Try It Online #
https://dotnetfiddle.net/
https://blazorrepl.telerik.com/
Fun Facts #
- Razor syntax is designed to be intuitive for C# developers who also write HTML.
- It powers both traditional server-rendered apps and client-side Blazor apps.
- You can write entire web UIs in
.razor
files when building with Blazor.
Resources #
Official site
Docs or tutorial
GitHub or interpreter
Community or learning resources