Search code examples
c#.netasp.net-corenavigationblazor

Blazor navigation with string parameters issue


Im using .net 8 Blazor application with navigation using NavigationManager

var instanceIdForNavigation = Uri.UnescapeDataString(MyStringValue);
NavigationManager.NavigateTo($"/recipeInstanceDetail/{instanceIdForNavigation}");

This navigation results in url such as https://localhost:5103/recipeInstanceDetail/M-XXX.3_520kg_20231122125053_2

Everything works fine. However when i press F5 or just paste the URL i get 404.

My page have this as a first line

@page "/recipeInstanceDetail/{InstaceId}"

And in razor.cs file i do have property

 [Parameter]
 public string InstaceId { get; set; }.

What am i doing wrong ? I have the same setup on other pages where everything works, so my guess would be some special characters or the content of the string. Thanx for any suggestions.

I did try to url encode and decode the string but never got anything out of it.


Solution

  • The problem is the dot in the "InstaceId" string. This caused url to be recognized as a file "xxx.xxx". So it returned not found static file 404.
    You could change the "_Host.cshtml" route template @page "/" to @page "/{**path}" to make the dot path work. "**" means get all parameter. https://learn.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-8.0#route-templates