How to get Url Referrer in ASP.NET Core
By FoxLearn 12/27/2024 8:34:57 AM 691
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.
- Options Pattern In ASP.NET Core
- Implementing Rate Limiting in .NET
- IExceptionFilter in .NET Core
- Repository Pattern in .NET Core
- CRUD with Dapper in ASP.NET Core
- How to Implement Mediator Pattern in .NET
- How to use AutoMapper in ASP.NET Core
- How to fix 'asp-controller and asp-action attributes not working in areas'
Categories
Popular Posts
Motiv MUI React Admin Dashboard Template
11/19/2024
AdminKit Bootstrap 5 HTML5 UI Kits Template
11/17/2024
K-WD Tailwind CSS Admin Dashboard Template
11/17/2024