How to get Url Referrer in ASP.NET Core
By FoxLearn 12/27/2024 8:34:57 AM 1.04K
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 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
- How to Create a custom model validation attribute in ASP.NET Core
- How to disable ModelStateInvalidFilter in ASP.NET Core
- How to fix LoginPath not working in ASP.NET Core
- Synchronous operations are disallowed
Categories
Popular Posts
Freedash bootstrap lite
11/13/2024
Admin BSB Free Bootstrap Admin Dashboard
11/14/2024
K-WD Tailwind CSS Admin Dashboard Template
11/17/2024
Spica Admin Dashboard Template
11/18/2024