-
Notifications
You must be signed in to change notification settings - Fork 10k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Route parameters with non-string values fails #48866
Comments
@Markz878 thanks for contacting us. Yes, this seems to be a bug (more of an integration issue). @surayya-MS @SteveSandersonMS this is very likely a difference between asp.net core routing and Blazor routing. Blazor will use the constraints to cast the value to the proper type, while ASP.NET Core won't do so. I suggest two possible action plans:
|
Most likely we'd have to do it in It would still technically be breaking because the following component works in .NET 7 but would start throwing: @page "/counter/{currentCount:int?}"
<p role="status">Current count: @_currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
int _currentCount;
public override Task SetParametersAsync(ParameterView parameters)
{
_currentCount = parameters.GetValueOrDefault<int>("currentCount");
return base.SetParametersAsync(ParameterView.Empty);
}
private void IncrementCount() => _currentCount++;
} If we want real back-compat we would just drop I'm not against requiring page components to use @javiercn Besides the perf optimization, are there functional requirements to have |
@SteveSandersonMS there are going to be inconsistencies with server-side routing and its going to block all the server-side routing features ASP.NET Core provides. I would suggest we make the change to align both routers in this case. It's a bit of an oddity that the Blazor router chose to cast the value for the route, even more when it's not saving an allocation, as it gets boxed anyway. I would suggest we move the code in there to |
Can you give examples? We absolutely can't have inconsistencies in page or parameter selection, because interactive If we do think there's a risk of unexpected inconsistencies, that seems like a further reason to prefer |
@SteveSandersonMS some examples: |
Thanks for pointing those out. It seems like the tradeoff is between:
Given the amount of uncertainty we're trying to manage as we head quickly towards the .NET 8 release, I tend to err on the side of constraining scope and maximizing correctness. When there isn't strong pressure to add a feature now, and we know we can't really deliver it fully, it's reassuring to scope it out. Would it be reasonable to add an |
We've discussed this offline and decided to go with the following approach. In parallel, we will see if we can bring the missing features to the routing system, so that this scenario works properly. If we manage to do that before rc2 and we are happy with the approach, we will take that and remove the temporary fix. Otherwise, we will come up with the proper fix in .NET 9, leaving the simple workaround in place in .NET 8. //cc @SteveSandersonMS , @javiercn, @surayya-MS |
Is there an existing issue for this?
Describe the bug
In .NET 8 preview 5, when creating a SSR Blazor app and using route parameters with e.g. int type, the page fails to load with an error: InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Int32'.
Here is the component I used to test this:
I tried this on .NET 7 Blazor WASM app and it worked.
Expected Behavior
When going to an url https://localhost:7208/counter/5 the CurrentCount property should be set to 5.
Steps To Reproduce
Create a .NET 8 Blazor SSR app, copy the code to a Counter.razor component and navigate to it's url.
.NET Version
8.0.100-preview.5.23303.2
The text was updated successfully, but these errors were encountered: