Unable to cast object of type 'System.String' to type 'System.Int32'. on Blazor/Razor routing parameters

2.1k Views Asked by At

I have tried to call a page with /somepage/{Id} where Id is a [Parameter] with a int property and the route is called as a string, it shouldn't be impicitly converting string to int? why it it wouldnt work at all? I am expecting it would recognize the parameter as it is...

what should I try to have the routing middleware to recognize the parameter ? even in MVC this works just fine...

the page routing

@page "/EditEmployee/{Id}"

the link

<a href="/EditEmployee/@Employee.EmployeeId" class="btn btn-primary m-1">Edit</a>

the parameter in the page

    [Parameter]
    public int Id { get; set; }

the result is an exception and the page doesnt load

1

There are 1 best solutions below

2
Henk Holterman On

Make it

 @page "/EditEmployee/{Id:int}"