Reputation: 973
Below block of code is Blazor documentation. But I am not able to figure out how and on what basis 'text' route parameter assigns value to the Text property.
@page "/RouteParameter"
@page "/RouteParameter/{text}"
<h1>Blazor is @Text!</h1>
@code {
[Parameter]
public string Text { get; set; }
protected override void OnInitialized()
{
Text = Text ?? "fantastic";
}
}
Upvotes: 1
Views: 2730
Reputation: 3811
The Blazor documentation you were reading is not intended to explain ALL of routing. It only goes so far as to explain how Blazor uses ASP.NET Core Routing.
In your Blazor Web App's Startup.cs file you'll see something like the following:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
MapBlazorHub()
is how Blazor ties into the ASP.NET Core Routing.
Example:
MVC -> [Route("customer/{id:int}")]
(attribute on a controller)
Razor Pages -> @page "/customer/{id:int}"
(page directive)
Blazor -> @page "/customer/{id:int}"
(page directive)
Example route: @page "/customer/{id:int}"
"{id:int}"
are processed by Route Constraints, and accepted or rejected, and in this case, converted to a strongly typed int
1
as a parameter.Warning : this is high level and glossing over some key things, please continue reading More on ASP.NET Core Routing
Upvotes: 3
Reputation: 31585
Blazor matches route param with properties by ignoring the word's case.
Blazor gets all parameters from the route and all properties from your class and assigns it ignoring the case.
So the lower case text
goes into Text
.
This would also work
@page "/RouteParameter"
@page "/RouteParameter/{Text}" // upper case Text
<h1>Blazor is @Text!</h1>
@code {
[Parameter]
public string Text { get; set; }
protected override void OnInitialized()
{
Text = Text ?? "fantastic";
}
}
But because normally the url doesn't have upper case, and C# properties starts with upper case, they maybe this comparison ignoring case.
Upvotes: 2