How do I get the page referrer in Blazor?

Platform: Blazor|Category : General, Components

To get the page referrer of where the user came from, follow these steps:

  1. Create a JavaScript function as a separate file in your application (~/wwwroot/interop.js).

    function getPageReferrer()
      {
         return document.referrer;
     }
    
  2. Add the script reference in the ~/wwwroot/index.html file of the Blazor WebAssembly application.

     <script src="interop.js"></script>
    
  3. Invoke the JavaScript function using the IJSRuntime service in a Razor page.

     @page "/"
     @inject IJSRuntime JSRuntime
     @code { 
     protected override async Task OnInitializedAsync()
     {
    	var pageReferrer = await JSRuntime.InvokeAsync<string>("getPageReferrer"); 
     }
     } 
    

    View Sample in GitHub