title: Blazor Navigationmanager cancel navigation on locationchanged
body: <h1>Question:</h1>
  <p>Is there a way on Blazor to cancel navigation?</p>
  <p>Let's supose a siple <code>a href</code> like this:</p>
  <pre class="lang-html prettyprint-override"><code>&lt;a href=&quot;/some/other/blazor/spa/page&quot;&gt;Go&lt;/a&gt;
  </code></pre>
  <p>I would like to cancel navigation (for example if we are editing a form)</p>
  <h1>What I have tried.</h1>
  <h3>Via JS</h3>
  <p>I set <code>onbeforeunload</code> dom delegate via JSInterop:</p>
  <pre class="lang-js prettyprint-override"><code>window.onbeforeunload = function(){
          return 'Vols abandonar aquesta pàgina?';
        };
  </code></pre>
  <p>but blazor bypass it.</p>
  <h3>Via locationchanged</h3>
  <p>I tried unsuccessfully to intercept navigation on <code>locationchanged</code> event:</p>
  <pre><code>@implements IDisposable
  @inject NavigationManager NavigationManager
  
  ...
  
  protected override void OnInitialized()
  {
      NavigationManager.LocationChanged += HandleLocationChanged;
  }
  
  private void HandleLocationChanged(object sender, LocationChangedEventArgs e)
  {
      // Cancel Navigation Here
  }
  
  public void Dispose()
  {
      NavigationManager.LocationChanged -= HandleLocationChanged;
  }
  </code></pre>