Search code examples
blazorblazor-webassembly

Use of navigation manager over href in blazor web-assembly


In Blazor web-assembly, is there a reason why should I use NavigationManager over a plain a href link structure?

For example

<a href="/test">Test</a>

<a @onclick="@LinkToTest">Test</a> or
<button @onclick="@LinkToTest">Test</button>

@code
{
void LinkToTest()
{
NavigationManager.NavigateTo("/test");
}
}

Is there a difference how the app would be rendered or routed?


Solution

  • Looking inside the source code for NavigationManager.NavigateTo, it just invokes an internal javascript function using the JsInterop. Looking at the source on the javascript side, that basically just sets location.href. So I think it's safe to say that calling NavigateTo is equivalent to just using a plain href.