How to get Url Referrer in ASP.NET Core
By FoxLearn 12/27/2024 8:34:57 AM 56
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 get HttpContext.Current in ASP.NET Core
- How to read Configuration Values from appsettings.json in ASP.NET Core
- How to add link parameter to asp tag helpers in ASP.NET Core
- How to set json serializer settings in ASP.NET Core
- Error 0x80004005 when starting ASP.NET .NET Core 2.0 in IIS
- How to receive a request with CSV data in ASP.NET Core
- How to use CORS in ASP.NET Core
- How to Send Emails in ASP.NET Core
Categories
Popular Posts
How to disable Windows Defender SmartScreen
12/24/2024
Improve Blazor Website Performance
12/19/2024