What is a Razor component, and what does a .razor file contain?

Last updated 5 July 2026

A Razor component is the fundamental building block of a Blazor UI: a reusable unit combining HTML markup and C# logic. It lives in a .razor file that the compiler turns into a C# class deriving from ComponentBase.

  • A .razor file typically contains:

    • Directives at the top (@page, @using, @inject, etc.).

    • Razor markup: HTML mixed with C# expressions via @ (e.g. @count, @if, @foreach).

    • An @code block holding fields, properties, parameters, and methods.

  • Naming and compilation: The file name becomes the component/tag name (Counter.razor → <Counter />), so it must start with an uppercase letter.

[lang:razor] @* Counter.razor *@ <p>Count: @count</p> <button @onclick="Increment">+</button> @code { private int count = 0; private void Increment() => count++; }

  • https://www.learnblazor.com/razor-components
  • https://learn.microsoft.com/en-us/aspnet/core/blazor/components/?view=aspnetcore-10.0
  • https://www.asptoday.com/p/exploring-aspnet-core-razor-components
  • https://medium.com/informatics/asp-net-core-blazor-components-razor-file-c6e4cd07dadf
  • https://learn.microsoft.com/en-us/aspnet/core/blazor/components/render-modes?view=aspnetcore-10.0
  • https://learn.microsoft.com/en-us/aspnet/core/blazor/?view=aspnetcore-10.0
  • https://aubreydrescher.medium.com/transitioning-from-razor-pages-to-blazor-ac7f58821e57
  • https://www.adaface.com/blog/blazor-interview-questions/
All Blazor interview questions