Skip to content

Blazor Relative URLs in components / Sub-navigation issues #23615

Description

@Emma37

When trying to create components that use relative URLs in the href I am always directed back to the base url rather than relatively, e.g. base-url/page instead of base-url/other/things/page

I've tried href="page", href="./page" and "href="~/page" as I saw these suggested but the only thing I've found that works is to use a Navigation Manger to directly access the current URL and then add to this - but this feels over the top?

I originally thought it was because I was using a NavLink component but I am also getting it with simple anchor tags. So I think it is incorrect behaviour.

Additional context/further feature request

The context where this has come up is trying the build a subnav component that we can re-use across different subsites/contexts. This is why using an absolute path isn't an option.

The site has the structure:
base-url/sub-site/page/sub-page

So the subnavigation needs to be able to handle any previous url base-url/sub-site-A/page-B/, base-url/subsite-C/page-D etc. but it provides all the same suffixes across these various prefixes.

Simply capturing the current URL and adding to it then caused issues like base-url/sub-site/page/sub-page/sub-page
and trying to remove parts of the URL might work but gets complicated when I consider more complex routes like base-url/sub-site/page/sub-page/sub-sub-page/... so that's not extensible either

So far I have made a workaround by passing the required relative base URL to the subnav component through a cascading parameter, i.e. relativeBase = “base-url/sub-site/page/”, then basing all hrefs in the subnav off of this new base.

However this all seems like I am missing something or that this is harder than it needs to be. Could there potentially be a SubNavLink component which could handle this kind of functionality: matching on some portion of the URL or have some ability to pattern match on the URL?

Thanks!

Activity

  1. added
    area-blazor
    on Jul 2, 2020
  2. added
    component ecosystem
    enhancement
    on Jul 6, 2020
  3. added this to the Backlog milestone on Jul 6, 2020
  4. mkArtakMSFT commented on Jul 6, 2020

    @mkArtakMSFT
    Contributor

    We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

  5. mkArtakMSFT commented on Jul 6, 2020

    @mkArtakMSFT
    Contributor

    Hi @Emma37. Just wanted to share some context here. The current behavior is by-design. However we did receive similar feedback in the past. We plan to revisit this issue when we will be planning for .NET 6 and see whether there is something we can do to help in this space.

  6. removed
    component ecosystem
    on Jul 6, 2020
  7. added
    affected-few
    severity-minor
    on Oct 7, 2020
  8. Trapulo commented on Apr 12, 2021

    @Trapulo

    I also think that is not natural this behavior, and it creates some problem creating internal portable references...

  9. added
    feature-blazor-component-model
    on Apr 20, 2021
  10. Felix-CodingClimber commented on Nov 12, 2023

    @Felix-CodingClimber

    This feature would help a lot with content generated from markdown. Almost all markdown parsers use relative links for footnote and header links.

  11. deleted a comment from on Nov 20, 2023
  12. GiovanniNova commented on Feb 1, 2024

    @GiovanniNova

    Any news on this? I'm on .NET 7.0 and NavigationManager.NavigateTo also displays this unnatural behaviour, afaik only URLs that start with a forward slash (/) should be treated as absolute

  13. changingmission commented on Feb 2, 2024

    @changingmission

    Same issue with SSR Blazor

  14. alfattack commented on Oct 29, 2024

    @alfattack

    Any updates on this?

  15. nicholasyin commented on Mar 23, 2025

    @nicholasyin

    As always, it's been ignored, even though so many developers think this design anti-intuitive and inconsistent with normal html knowledge.

  16. oakad commented on Aug 12, 2025

    @oakad

    Not only this issue is counter intuitive, Google AI and some other web resources think that it works correctly, that is, relative paths are resolved relative to current page URI, thus misleading unlucky developers.

    Official documentation mentions the "relative to the base URI" bit in regular print, easy to miss: https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.components.navigationmanager.navigateto?view=aspnetcore-9.0

    This is quite bad, because not many people have the capacity to test each and every link in their application.

  17. nicholasyin commented on Aug 21, 2025

    @nicholasyin

    Don't ever expect this will change, because the current behavior has been there for generations of .NET. MS is impossible to revert the behavior as it will break all existing code.

  18. oakad commented on Aug 21, 2025

    @oakad

    However, navigation manager could provide a helper, in a form of extension method or something like that. I, for one, ended up coding my own. A decent posix path helper will also be handy, we are forced to maintain custom ones, because IO.Path cannot be trusted for such things.

    Navigation.NavigateTo(PosixPath.Join(
    	Navigation.ToBaseRelativePath(Navigation.Uri),
    	relPath
    ));
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    affected-fewarea-blazorenhancementfeature-blazor-component-modelseverity-minor

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions