ASP.NET Core razor components
In this article we are going to discuss on creating effective and reusable asp.net core razor components.
Requesting you to review my previous articles on Blazor
- Components are the building blocks of a blazor application.
- Razor components can be nested, reused, and if implemented properly, can be shared across multiple projects.
- Razor component files have the extension .razor
Let us go ahead and look at a razor component example. Counter.razor is a component which we can see when we are creating a blazor project.
<p role=”status”>Current count: @currentCount</p>
<button class=”btn btn-primary” @onclick=”IncrementCount”>Click me</button>
private int currentCount = 0;
private void IncrementCount()
The razor component is a mix of the below two things
- HTML markup which defines the look and feel of the component.
- C# Code which defines the processing logic
Let us deep dive into the example and understand each line
- C# Code is in @code block. We can have more than one @code blocks
- The C # code increments currentCount variable by 1 every time when we click on the button.
- The method IncrementCount() is wired up using the attribute onclick attribute. When the button is clicked, this function will be invoked.
- In HTML, to access the private variable currentCount, use the character – @.
- While compiling the application, the HTML and C# code will be converted into a component class.
- The name of the generated class matches the name of the component file.
- Component file name start with an Uppercase character. If we add a component file -start with lower case letter, the code will fail to compile, and we get the compiler error – “Component names cannot start with a lowercase character”
In case of Blazor Server project,
- Blazor server projects will run on the server.
- A SignalR connection will be established between server and client browser.
- The click event information will be sent to server over the SignalR connection.
- In response to the event, the component is regenerated, but the entire HTML is not sent back to the client. Here, only the new counter value will be sent back to browser.
- Instead of reloading and updating the entire page, only the changed part of the page is updated. This leads the application feels more response and faster.
Razor component rendering
In the example, Counter component rendered by navigating to /counter in the browser. The path is specified at the top of the component using the directive @page
Razor component nesting
A component can be nested inside another component using HTML syntax. For an instance, to nest the Counter component in Index component using the below approach
The entire Index component as below.
Welcome to your new app.
Nested Component <Counter/>
<SurveyPrompt Title=”How is Blazor working for you?” />
Best Practice to add components
Components can be added anywhere in a blazor project. However, it would be a good practice to add components that generate webpages in the Pages folder and reusable non-page components in the Shared folder. It is very much possible to add the components in custom folders depends on your wish.
Separate component into HTML and C# code
In the example, Counter.razor, both HTML and C# code are in a single file. However, it would be a good practice to keep HTML and C# code into separate files. It is not only easy for maintenance but also good for unit testing. There are two approaches through which we can separate the components into HTML and C# code.
- Partial Files Approach
- Base class Approach
I will explaining these two approach in the next article.