BLAZOR ROUTE PARAMETER EXCEPTION

MOON 60 Reputation points
2025-12-13T05:40:11.4233333+00:00

Current page directive of Blazor component is like:

@page /mypage/{thestate:bool?}

When I call the URL localhost:32360/mypage, there is no problem; nevertheless, when I call the URL localhost:32360/mypage/false or localhost:32360/mypage/true, the web browser prints that error report to its console:log

Blazor Server, in addition, does not respond user interactions in the page after the exception occurs.

Developer technologies | ASP.NET | ASP.NET Core
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Marcin Policht 68,615 Reputation points MVP Volunteer Moderator
    2025-12-13T12:18:27.3666667+00:00

    As far as I can tell, the issue is not with Blazor Server itself, but with how the route parameter is defined and bound. In Blazor, route templates should be enclosed in quotes, and the route parameter must match a [Parameter] property on the component with a compatible type. If either of these is wrong, Blazor throws a routing exception, which breaks the server circuit and causes the UI to stop responding.

    Your directive is missing quotes, so the route constraint is not parsed correctly. It should be written as a string literal.

    @page "/mypage/{thestate:bool?}"
    

    In addition, the component must declare a matching parameter. Because the route allows the value to be optional (bool?), the property must also be nullable.

    @code {
        [Parameter]
        public bool? thestate { get; set; }
    }
    

    With this setup:

    /mypagethestate == null

    /mypage/truethestate == true

    /mypage/falsethestate == false

    If the parameter type is non-nullable (bool) while the route allows it to be optional, or if the route syntax is invalid, Blazor throws an exception during routing. In Blazor Server, any unhandled exception during rendering terminates the circuit, which is why user interactions stop working afterward.


    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.