





















































Hi ,
Welcome to this week’s edition of ProgrammingPro!
In today’sExpert Insight, we bring you an excerpt from the recently published book, C# 13 and .NET 9 – Modern Cross-Platform Development Fundamentals - Ninth Edition, which demonstrates defining routable page components, passing route parameters, and navigating routes in Blazor.
News Highlights:Kotlin 2.1.0 previews advanced when expressions; Rust 1.83 enhances Multiplatform and Gradle tools; AWS Q Developer adds AI-driven testing and modernization; and Oracle launches GraalVM JIT for its JDK only.
My top 5 picks from today’s learning resources:
But there’s more, so dive right in.
Stay Awesome!
Divya Anne Selvaraj
Editor-in-Chief
when
expressions, non-local breaks/continues, and multi-dollar string interpolation as preview features.LinkedList
class and more.var
, let
, const
, function declarations, and function expressions.rescue
function which offers a concise and readable way to handle exceptions without disrupting application flow.Here’s an excerpt from “Chapter 14: Building Interactive Web Components Using Blazor" in the book, C# 13 and .NET 9 – Modern Cross-Platform Development Fundamentals - Ninth Edition by Mark J. Price, published in November 2024.
To create a routable page component, add the@page
directive to the top of a component’s.razor
file, as shown in the following markup:
@page "/customers"
The preceding code is the equivalent of a mapped endpoint inProgram.cs
, as shown in the following code:
app.MapGet("/customers", () => ...);
TheRouter
component scans the assembly specifically in itsAppAssembly
parameter for Blazor components with the@page
directive, registering their URL paths as endpoints.
At runtime, a page component is merged with any specific layout that you have specified in theRoutes.razor
file<RouteView>
component. By default, the Blazor Web App project template definesMainLayout.razor
as the layout for page components.
Pro Tip:
Good Practice: By convention, put routable page Blazor components in theComponents\Pages
folder.
Microsoft provides a dependency service namedNavigationManager
that understands Blazor routing and theNavLink
component. TheNavigateTo
method is used to go to the specified URL.
Blazor routes can include case-insensitive named parameters, and your code can most easily access the values passed by binding the parameter to a property in the code block, using the[Parameter]
attribute, as shown in the following markup:
@page "/customers/{country}"
<div>Country parameter as the value: @Country</div>
@code {
[Parameter]
public string Country { get; set; }
}
The recommended way to handle a parameter that should have a default value when it is missing is to suffix the parameter with?
and use the null-coalescing operator in theOnParametersSet
method, as shown in the following markup:
@page "/customers/{country?}"
<div>Country parameter as the value: @Country</div>
@code {
[Parameter]
public string Country { get; set; }
protected override void OnParametersSet()
{
// If the automatically set property is null, then
// set its value to USA.
Country = Country ?? "USA";
}
}
C# 13 and .NET 9 – Modern Cross-Platform Development Fundamentals - Ninth Editionwas published in Novemver 2024. Packt library subscribers can continue reading the entire book for free or you can buy the book