Blazor Components in Razor Page: Zero To Hero in Blazor with Real Time Implementation Part VI


This is part six of the “Blazor: Zero to Hero” article series. Before getting into the Part VI article series, let’s have a Glimpse at previous articles in this series, 

Part V

  • Blazor is a component driven framework, i.e. Components are the fundamental build blocks of a Blazor application
  • Components can be reused and shared across multiple projects
  • The extension of the component file is .razor. This is the reason we term it as Razor Components or Blazor Components
  • Component name always start with Upper Case Character. If we name it in lower case, then we get an error message as “Component names cannot start in Lower Case Character.”

Please visit for complete article, 

In this article, We will discuss about the Nested Components and Split razor components into Sperate Files in the Blazor Project.

Nested Components

  • Components can be nested under another components
  • To nest one component, we use HTML syntax like <ComponentName/>. Here <ComponentName/> denotes the name of the Component to be nested.

Lets take our Counter.razor component, and lets nest this into the Index.razor. Inorder to nest, we should include the instance of the Counter.razor component as <Counter/> in Index.razor.

Fig. 1 Counter.razor component nested in Index.razor component
Fig. 2 Output of Index.razor Controller

Components can be placed anywhere in a Blazor project. The Good practice is to keep the web pages under the Pages Folder in the Project.

Fig. 3 Under Pages Folder all components are placed.

The reusable and non-page components are placed under the Shared Folder. We can also customize the folders based on the Project.

Split Razor Components

Razor components are combination of both HTML and C# Code. In the above example, both the HTML and C# code are present in a single file, this is fine since our component is simple one. For more complex components, its good practice to keep the HTML and C# code in a separate files.

There are two approaches to achieve this,

  • Partial Files
  • Base Class
Partial Files
  • The HTML part in the component remains in the same .razor file. Example, the HTML code in the Counter.razor will remain in the same component.
  • The C# code will be kept in .razor.cs file. Example, the C# code of the Counter.razor will be kept in the Counter.razor.cs file

So, in the Pages folder, let’s create a new class as Counter.razor.cs file.

Fig.4 Class file is nested under the component file

Now let’s move the C# code from the razor component to the Class file.

Fig.5 Counter.razor.cs File
Fig.6 View File

In order to access the Counter Class members in the razor component (View File), we need to make Counter Class as Partial Class. At the compile time Partial Class and generated partial class of the razor component (view file) will be combined, so that the Properties and Methods in the class file will be available to the view file.

Base Class

Let’s add a new class files to the Pages folder and name it as CounterBase.cs. The reason to name it as Base because this class is going to be the Base Class for the Counter Component Class.

  • To make Base Class for a component, we need to inherit build in Component Base Class. This class is available in Microsoft’s ASPNETCORE.Components namespace.
  • Now let’s include our C# Code in this class.
  • We need to tell our razor component (view file) to inherit from the base class, to do that we need to include @inherits directive in the razor component, and then specify the Base Class that to be Inherited.
Fig.7 Component File with @inherits Directive
Fig. 8 Base Class
  • At this point if we build our application we will get some errors as shown below,
Fig.9 Errors during build time
  • The reason we get this error is because all the members in the base class are defined as Private access modifier. The derived class should be able to access the members only if its in Protected access modifier.
  • Let’s change all the Private access modifiers to Protected access modifiers. Once we build the application, we will have successful build and run the application.
Fig.10 Razor Component in Browser
  • In both Partial Files and Base Class the application runs as the same way before.


There are two ways to split the HTML and C# code into their own separate files. Hope you all found this article pretty useful. Let’s have a detailed view on other concepts in Blazor in upcoming article. Please feel free to share your feedbacks in the comment section.

5 thoughts on “Blazor Components in Razor Page: Zero To Hero in Blazor with Real Time Implementation Part VI

Add yours

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Website Built with

Up ↑

%d bloggers like this: