Storing data in the URL
At first glance, this option might sound horrific, but it’s not. Data, in this case, can be the blog post ID or the page number if we are using paging. Typically, the things you want to save in the URL are things you want to be able to link to later on, such as blog posts in our case.
To read a parameter from the URL, we use the following syntax:
@page “/post/{BlogPostId:int}”
The URL is post followed by Id of the post.
To find that particular route, BlogPostId must be an integer, otherwise the route won’t be found.
We also need a public parameter with the same name:
[Parameter]
public int BlogPostId{ get; set; }
If we store data in the URL, we need to make sure to use the OnParametersSet or OnParametersSetAsync methods, otherwise the data won’t get reloaded if we change the parameter. If the parameter changes, Blazor won’t run OnInitializedAsync again.
This is why our post.razor component loads the things that...