Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 3995

NavigationManager - Get current URL in a Blazor component

$
0
0

I need to know the URL of the current page in order to check if I have to apply a certain style to an element. The code below is an example.

    @using Microsoft.AspNetCore.Blazor.Services    @inject IUriHelper UriHelper    @implements IDisposable<h1>@url</h1><nav><div class="nav-wrapper"><a href="#" class="brand-logo">Blazor</a><ul id="nav-mobile" class="right hide-on-med-and-down"><li><NavLink href="/" Match=NavLinkMatch.All>                        Home</NavLink></li><li><NavLink href="/counter">                        Counter</NavLink></li><li><NavLink href="/fetchdata">                        Fetch data</NavLink></li></ul></div></nav>    @functions {        private string url = string.Empty;        protected override void OnInit()        {            url = UriHelper.GetAbsoluteUri();            UriHelper.OnLocationChanged += OnLocationChanged;        }        private void OnLocationChanged(object sender, LocationChangedEventArgs e)        {            url = newUriAbsolute;        }        public void Dispose()        {            UriHelper.OnLocationChanged -= OnLocationChanged;        }    }

I used the same approach used in the NavLink component in the Blazor repository, but it did not work. Any ideas?.


Viewing all articles
Browse latest Browse all 3995

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>