httprequestblazorwebassemblyreferer

Blazor webassembly: detect from which site the user came from


I am developing a Blazor Webassembly application. Based on the page the user came from, I should show different content. For example, the app should show Content A when the user came from Facebook and Content B otherwise. How can this feature achieved?

I red that one could do this by checking the referrer property of the request header, but I don't know how to access the http request on a razor page.


Solution

  • You can use javascript for this.

    1) Create a javascript file on your blazor project (the front-end) with the following content:

    function getReferrer() {
        return document.referrer;
    }
    

    2) Add the previous file to your index.html file:

    <script src="JavaScript.js"></script>
    

    3) Consume the function using the IJSRuntime service:

    var referrer = await js.InvokeAsync<string>("getReferrer");
    

    This should get you the referrer.