In Blazor, when a route parameter constraint is applied, the value is automatically cast to the corresponding component parameter type. If the constraint type does not match the component parameter type, it can lead to confusion and potential runtime errors due to unsuccessful casting. Therefore, it is crucial to ensure that the types of route parameters and component parameters match to prevent such issues and maintain code clarity.
Ensure the component parameter type matches the route parameter constraint type.
| Constraint Type | .NET Type |
|---|---|
bool |
bool |
datetime |
DateTime |
decimal |
decimal |
double |
double |
float |
float |
guid |
Guid |
int |
int |
long |
long |
string |
string |
@page "/my-route/{Param:datetime}"
@code {
[Parameter]
public string Param { get; set; } // Noncompliant
}
@page "/my-route/{Param:datetime}"
@code {
[Parameter]
public DateTime Param { get; set; } // Compliant
}