title: Get current URL in a Blazor component
body: <p>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.</p>
  <pre class="lang-html prettyprint-override"><code>    @using Microsoft.AspNetCore.Blazor.Services
      @inject IUriHelper UriHelper
      @implements IDisposable
  
      &lt;h1&gt;@url&lt;/h1&gt;
      &lt;nav&gt;
          &lt;div class=&quot;nav-wrapper&quot;&gt;
              &lt;a href=&quot;#&quot; class=&quot;brand-logo&quot;&gt;Blazor&lt;/a&gt;
              &lt;ul id=&quot;nav-mobile&quot; class=&quot;right hide-on-med-and-down&quot;&gt;
                  &lt;li&gt;
                      &lt;NavLink href=&quot;/&quot; Match=NavLinkMatch.All&gt;
                          Home
                      &lt;/NavLink&gt;
                  &lt;/li&gt;
                  &lt;li&gt;
                      &lt;NavLink href=&quot;/counter&quot;&gt;
                          Counter
                      &lt;/NavLink&gt;
                  &lt;/li&gt;
                  &lt;li&gt;
                      &lt;NavLink href=&quot;/fetchdata&quot;&gt;
                          Fetch data
                      &lt;/NavLink&gt;
                  &lt;/li&gt;
              &lt;/ul&gt;
          &lt;/div&gt;
      &lt;/nav&gt;
  
      @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;
          }
      }
  </code></pre>
  <p>I used the same approach used in the NavLink component in the Blazor repository, but it did not work. Any ideas?.</p>