How to get Url Referrer in ASP.NET Core
By Tan Lee Published on Dec 27, 2024 1.68K
In ASP.NET Core, the referrer URL (the page that made the request) can be retrieved from the `Request.Headers` collection, where it is stored under the `Referer` header.
The StringValues
class in ASP.NET Core is used to efficiently represent strings, particularly in the HttpContext
object. To convert a StringValues
instance to a regular string, you can simply call its ToString()
method.
using Microsoft.AspNetCore.Mvc; public class HomeController : Controller { public IActionResult Index() { // Get the Referer (Referrer) header from the request var referrer = Request.Headers["Referer"].ToString(); // Use the referrer URL as needed ViewData["Referrer"] = referrer; return View(); } }
The Referer
header is optional. If the request doesn't include it (for example, when a user navigates directly to a page or if it has been stripped out for privacy reasons), the value will be an empty string.
- How to Initialize TagHelpers in ASP.NET Core with Shared Data
- Boost Your ASP.NET Core Website Performance with .NET Profiler
- The name 'Session' does not exist in the current context
- Implementing Two-Factor Authentication with Google Authenticator in ASP.NET Core
- How to securely reverse-proxy ASP.NET Core
- How to Retrieve Client IP in ASP.NET Core Behind a Reverse Proxy
- Only one parameter per action may be bound from body in ASP.NET Core
- The request matched multiple endpoints in ASP.NET Core
Categories
Popular Posts
Material Lite Admin Template
Nov 14, 2024
11 Things You Didn't Know About Cloudflare
Dec 19, 2024
Toolbox Admin Responsive Tailwind CSS Admin Template
Nov 20, 2024
Focus Admin Dashboard Template
Nov 18, 2024