Kontext Kontext / Code Snippets & Tips

ASP.NET Core Find out Page Referer

event 2022-08-31 visibility 1,213 comment 0 insights
insights Stats

Code description

Referer is usually sent as part of HTTP request headers. For instance, the Referer field in HTTP request header can look like the following:

Referer: https://kontext.tech/

This code snippet provides example of retrieving referer information via HttpRequest.GetTypedHeaders method in ASP.NET Core Razor pages. It also shows you how to get the relative path and query string only. The same code can also be used in web API controllers.

Note: The referer can be NULL if user directly type the page URL in browser. 

Code snippet

public IActionResult OnGet()
    var headers = Request.GetTypedHeaders();
    var referer = headers.Referer;
    var refererRelativePath = referer?.PathAndQuery;
    return Page();
More from Kontext
comment Comments
No comments yet.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts