In this step, you will update the existing Home
component. The Home
component will be the first page of your portfolio site. It will include your name, a brief introduction about yourself, and a photo of yourself.
Blazor apps are built using components. A component is a self-contained chunk of user interface (UI), such as a page, dialog, or form. A component includes HTML markup and the processing logic to enable dynamic behaviours like rendering the UI. The components can be nested, reused and shared among other projects.
OK. Let's get started!
If you haven't completed the previous step or want to start from the save point, run the following commands to restore the Blazor WebAssembly project.
cd $CODESPACE_VSCODE_FOLDER
mkdir -p workshop && cp -a save-points/step-01/. workshop/
cd workshop
We need to clean up the default layout to make our portfolio site look better.
-
Make sure you're in the
workshop
directory.cd $CODESPACE_VSCODE_FOLDER/workshop
-
Delete the files by running the following commands or on the codespace workspace on the side.
rm MyPortfolio/Layout/NavMenu.razor rm MyPortfolio/Layout/NavMenu.razor.css rm MyPortfolio/Pages/Counter.razor rm MyPortfolio/Pages/Weather.razor rm MyPortfolio/wwwroot/sample-data/weather.json
-
Update the
MainLayout.razor
file to remove the navigation menu. OpenMyPortfolio/Layout/MainLayout.razor
and replace the content with the following code:@inherits LayoutComponentBase <div id="main"> @Body </div>
-
Copy the site layout CSS from the
docs/css
directory to theMyPortfolio/wwwroot/css
directory.mkdir -p $CODESPACE_VSCODE_FOLDER/workshop/MyPortfolio/wwwroot/css cp -a $CODESPACE_VSCODE_FOLDER/docs/css/. $CODESPACE_VSCODE_FOLDER/workshop/MyPortfolio/wwwroot/css/
-
Copy the relevant images from the
docs/images
directory to theMyPortfolio/wwwroot/images
directory.mkdir -p $CODESPACE_VSCODE_FOLDER/workshop/MyPortfolio/wwwroot/images cp -a $CODESPACE_VSCODE_FOLDER/docs/images/. $CODESPACE_VSCODE_FOLDER/workshop/MyPortfolio/wwwroot/images/
Since there is already the Home
component, we will update the Home
component to include your name, a brief introduction about yourself, and a photo of yourself.
-
Open
MyPortfolio/Pages/Home.razor
and update the content with the following code:@page "/" @using System.Net.Http.Json @using MyPortfolio.Models @inject HttpClient Http <PageTitle>My Portfolio</PageTitle>
-
Below the
PageTitle
component, add the following code. This is the HTML part of theHome
component.@page "/" @using System.Net.Http.Json @using MyPortfolio.Models @inject HttpClient Http <PageTitle>My Portfolio</PageTitle> @* ⬇️⬇️⬇️ Add codes below ⬇️⬇️⬇️ *@ <section class="dark" id="home"> @if (heroHome is not null) { <img class="background" src="@(heroHome.Src)" alt="@(heroHome.Alt)" /> } <div style="position: absolute; top: 30%; left: 2rem;"> @if (property is null) { <p><em>Loading...</em></p> } else { <h1>@property.Name</h1> <h2>@property.Title</h2> } </div> <div style="position: absolute; bottom: 8rem; left: 50%;"> <a href="#about" target="_top"> <img src="images/down-arrow.svg" style="height: 3rem; width: 3rem;" alt="scroll down" /> </a> </div> </section>
-
Below the
</section>
tag, add the following code. This is the C# code that handles the UI logic. The logic reads bothsiteproperties.json
andheroimages.json
files to display the hero image and the site properties.</section> @* ⬇️⬇️⬇️ Add codes below ⬇️⬇️⬇️ *@ @code { private SiteProperties? property; private HeroImage? heroHome; protected override async Task OnInitializedAsync() { property = await Http.GetFromJsonAsync<SiteProperties>("sample-data/siteproperties.json"); var heros = await Http.GetFromJsonAsync<List<HeroImage>>("sample-data/heroimages.json"); heroHome = heros.SingleOrDefault(h => h.Name == "home"); } }
Both siteproperties.json
and heroimages.json
files are base data to display the hero image and the site properties. We will add these files to the wwwroot/sample-data
directory.
-
Make sure you're in the
workshop
directory.cd $CODESPACE_VSCODE_FOLDER/workshop
-
Create both files in the
wwwroot/sample-data
directory by running the following commands or on the codespace workspace on the side.touch MyPortfolio/wwwroot/sample-data/siteproperties.json touch MyPortfolio/wwwroot/sample-data/heroimages.json
-
Open
siteproperties.json
and add the following content:{ "name": "{{your-name}}", "title": "{{your-title}}" }
-
Open
heroimages.json
and add the following content:[ { "name": "home", "src": "images/woman-with-tablet.jpg", "alt": "woman with glasses holding a tablet" } ]
Reading JSON data in Blazor is done by creating classes that represent the JSON data. We will create SiteProperties
and HeroImage
classes to represent the siteproperties.json
and heroimages.json
files.
-
Make sure you're in the
workshop
directory.cd $CODESPACE_VSCODE_FOLDER/workshop
-
Create both
SiteProperties
andHeroImage
classes in theModels
directory by running the following commands or on the codespace workspace on the side.mkdir -p $CODESPACE_VSCODE_FOLDER/workshop/MyPortfolio/Models touch MyPortfolio/Models/SiteProperties.cs touch MyPortfolio/Models/HeroImage.cs
-
Open
SiteProperties.cs
and add the following content:namespace MyPortfolio.Models; public class SiteProperties { public string Name { get; set; } = string.Empty; public string Title { get; set; } = string.Empty; }
-
Open
HeroImage.cs
and add the following content:namespace MyPortfolio.Models; public class HeroImage { public string Name { get; set; } = string.Empty; public string Src { get; set; } = string.Empty; public string Alt { get; set; } = string.Empty; }
-
Make sure you're in the
workshop
directory.cd $CODESPACE_VSCODE_FOLDER/workshop
-
Build your Blazor WebAssembly project to make sure everything is working fine.
dotnet build
-
Run the Blazor WebAssembly project to see the updated application on your web browser.
dotnet watch run --project MyPortfolio
-
The updated application is open, and should look like below.
-
Stop the running application by pressing
Ctrl + C
in the terminal.
Congratulations! You have updated the Home
component to include your name, a brief introduction about yourself, and a photo of yourself. Let's create a new component for the About me
in the next step.