Blazor Server Components are the main building blocks for Blazor Server websites. Each component consists of:
@code{}
block class defines behaviourThis topic describes how to create and configure a Razor Component.
As mentioned before, Blazor Server Component is called Razor Component. From now on, we will use the term Razor Component to refer to Blazor Server Component.
You can download the example code used in this topic on GitHub.
To create a component, verify that you have met the following prerequisites:
dotnet new blazorserver -n <ProjectName> -lang "C#"
where <ProjectName>
is the name of your Blazor Server website.The easiest way to create a component is with the Visual Studio UI. You can also create a component manually.
To create a component using Visual Studio UI:
Page
folder, right-click and select Add => Razor Component.Although Visual Studio UI is the easiest way to create a Razor Component, you can also create a component manually. This section describes how to create a component within an existing Blazor Server project.
To create a new component manually:
Page
folder within Blazor Server project.dotnet new razorcomponent -n <ComponentName>
.By default, this command creates a component file, <ComponentName>.razor
.
The template is a block of HTML that tells Blazor Server how to render the component in your website. You can define a template for your component by adding HTML tags directly to the component.
You can declare component styles uses for it's template in three ways.
<style>
tagTo declare the styles for a component in an external file, add a <link>
tag to the HTML template of component.
<link href="css/helloworld.css" rel="stylesheet" />
To declare the isolated styles for a component, add a file named <ComponentName>.razor.css
in the same folder of the component.
To declare the style within the component using the <style>
tag.
<h1 class="welcome-internal">Welcome to Blazor School (internal css)</h1> <style> .welcome-internal { color: green; } </style>