Skip to content

Exception when navigating between Blazor pages with a [SupplyParameterFromQuery] of the same name but different type #52483

Closed
@MatElcome

Description

@MatElcome

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

If there are two Blazor pages with a query parameter of the same name, but different type, navigating between them will cause an exception to be thrown.

@page "/TestInt"
<a href="/TestGuid?p=47259a22-9fd8-4938-b40d-e5c8d8642a3d">TestGuid</a>
@code {
    [Parameter]
    [SupplyParameterFromQuery]
    public int? p { get; set; }
}
@page "/TestGuid"
<a href="/TestInt?p=123">TestInt</a>
@code {
    [Parameter]
    [SupplyParameterFromQuery]
    public Guid? p { get; set; }
}

In the above example blazor tries to parse 123 as Guid? when you navigate from /TestInt?p=123 to /TestGuid?p=47259a22-9fd8-4938-b40d-e5c8d8642a3d

Error: System.InvalidOperationException: Cannot parse the value '123' as type 'System.Nullable`1[System.Guid]' for 'p'.
   at Microsoft.AspNetCore.Components.Routing.UrlValueConstraint.TypedUrlValueConstraint`1.Parse(ReadOnlySpan`1 value, String destinationNameForMessage)
   at Microsoft.AspNetCore.Components.Routing.QueryParameterValueSupplier.GetQueryParameterValue(Type targetType, String queryParameterName)
   at Microsoft.AspNetCore.Components.ParameterView.CascadingParameterEnumerator.MoveNext()
   at Microsoft.AspNetCore.Components.Reflection.ComponentProperties.SetProperties(ParameterView& parameters, Object target)
   at Microsoft.AspNetCore.Components.ComponentBase.SetParametersAsync(ParameterView parameters)
   at Microsoft.AspNetCore.Components.Rendering.ComponentState.SupplyCombinedParameters(ParameterView directAndCascadingParameters)

Expected Behavior

Clicking a link to the other page should successfully navigate to it without throwing an exception.

Steps To Reproduce

An example project is located: here

  • Load up the project to the index page /
  • Click the link TestInt 123
  • Click the link TestGuid 47259a22-9fd8-4938-b40d-e5c8d8642a3d
  • An exception is thrown
    • An unhandled exception has occurred. See browser dev tools for details is displayed at the bottom of the page
    • The browser's console displays the exception stack trace

Exceptions (if any)

Error: System.InvalidOperationException: Cannot parse the value '123' as type 'System.Nullable`1[System.Guid]' for 'p'.
   at Microsoft.AspNetCore.Components.Routing.UrlValueConstraint.TypedUrlValueConstraint`1.Parse(ReadOnlySpan`1 value, String destinationNameForMessage)
   at Microsoft.AspNetCore.Components.Routing.QueryParameterValueSupplier.GetQueryParameterValue(Type targetType, String queryParameterName)
   at Microsoft.AspNetCore.Components.ParameterView.CascadingParameterEnumerator.MoveNext()
   at Microsoft.AspNetCore.Components.Reflection.ComponentProperties.SetProperties(ParameterView& parameters, Object target)
   at Microsoft.AspNetCore.Components.ComponentBase.SetParametersAsync(ParameterView parameters)
   at Microsoft.AspNetCore.Components.Rendering.ComponentState.SupplyCombinedParameters(ParameterView directAndCascadingParameters)

or if you visit the links in the reverse order:

Error: System.InvalidOperationException: Cannot parse the value '47259a22-9fd8-4938-b40d-e5c8d8642a3d' as type 'System.Nullable`1[System.Int32]' for 'p'.
   at Microsoft.AspNetCore.Components.Routing.UrlValueConstraint.TypedUrlValueConstraint`1.Parse(ReadOnlySpan`1 value, String destinationNameForMessage)
   at Microsoft.AspNetCore.Components.Routing.QueryParameterValueSupplier.GetQueryParameterValue(Type targetType, String queryParameterName)
   at Microsoft.AspNetCore.Components.Reflection.ComponentProperties.SetProperties(ParameterView& parameters, Object target)
   at Microsoft.AspNetCore.Components.ComponentBase.SetParametersAsync(ParameterView parameters)
   at Microsoft.AspNetCore.Components.Rendering.ComponentState.SupplyCombinedParameters(ParameterView directAndCascadingParameters)

.NET Version

8.0.100

Anything else?

This is a regression - it occurs in net8.0, but not if the project is downgraded to net7.0.

Metadata

Metadata

Assignees

Labels

area-blazorIncludes: Blazor, Razor ComponentsbugThis issue describes a behavior which is not expected - a bug.

Type

No type

Projects

Status

Done

Relationships

None yet

Development

No branches or pull requests

Issue actions