Skip to content

Blazor <a href> link in ShadowDOM causes page reload, correct way to redirect from href="javascript:"? #27070

Closed
@MichaelPeter

Description

@MichaelPeter

Hello AspNetCore Team,

I use a Html WebComponent for Navigation from our Design System which internally generates a

<a hef="SublinkA/SublinkB"

My browser shows me: localhost:55003/SublinkA/SublinkB as URL, localhost:55003 is my blazor webassembly page

If I press the link, Blazor does not route internally but instead the page gets reloaded to the new location.

Now if I put the Link into Blazor-NavLink obviously blazor redirects the link correctly without reloading the whole page.

Now the < a href > gets generated inside an html webcomponent where I don't have any Blazor NavLinks
but I can pass any url for example

< a href="javascript:RedirectTheBlazorWay()">bla</ >


Now instead of calling in the href:
javascript:DotNet.invokeMethodAsync("RedirectTheBlazorWay", controlRef, "SublinkA/SublinkB")

And Registering:
    [JSInvokable("RedirectTheBlazorWay")]
    public Task RedirectTheBlazorWay(string url)
    {
        NavigationManager.NavigateTo(url, forceLoad: false);
        return Task.CompletedTask;
    }

where RedirectTheBlazorWay needs to be a non static method because otherwise I cannot inject the NavigationManager
is there an easy way to redirect inside the blazor page?

Like javascript:DotNet.navigateTo(xyz)?

Or is there an error with my url that the link is not recognized as an local url.

Sorry if this was asked already but did not find any resources.
Just found this: https://chrissainty.com/an-in-depth-look-at-routing-in-blazor/

Metadata

Metadata

Labels

DoneThis issue has been fixedServicing-considerShiproom approval is required for the issuearea-blazorIncludes: Blazor, Razor ComponentsbugThis issue describes a behavior which is not expected - a bug.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions