question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Route parameters with non-string values fails

See original GitHub issue

Is there an existing issue for this?

  • I have searched the existing issues

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:

@page "/counter/{currentCount:int?}"
<p role="status">Current count: @CurrentCount</p>
@code {
    [Parameter] public int CurrentCount { get; set; }
}

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

Issue Analytics

  • State:closed
  • Created 3 months ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
javiercncommented, Jun 19, 2023

@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:

  • Process the parameters inside Router to convert the string to the appropriate type.
  • Update RouteView to do this casting instead (since it knows the parameter types).
  • Convert route values into cascading parameters and implement [SupplyParameterFromRoute].
0reactions
mkArtakMSFTcommented, Jun 21, 2023

We’ve discussed this offline and decided to go with the following approach. As a short-term mitigation we will make so that the client router will ignore the server router logic completely for now.

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Route parameters with non-string values fails
Route parameters with non-string values fails · Summary · Jobs. backport. cleanup · run_backport · Run details. Usage · Workflow file.
Read more >
Passing strings as route parameters in Angular without ...
Edit: The variable params is of type URLSearchParams() . This is how I'm trying to extract the parameter: this.route.paramMap.switchMap( ...
Read more >
How to Use @Input() to Read Angular Route Parameters
We add an input property using the @Input() decorator. @Input() id = '';. We can name the input property the same as the...
Read more >
MySQL Cookbook - Page 917 - Google Books Result
PATH environment variable error when invoking mysql, 4 solution, ... 879 nonstring values, 308 regular expressions (see regular expres‐sions) SQL patterns, ...
Read more >
sys — System-specific parameters and functions
This module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found