How can I get Url Referrer in ASP.NET Core MVC?

C#asp.net Coreasp.net Core-Mvc

C# Problem Overview


I am trying to migrate an ASP.NET MVC webform to ASP.NET Core MVC. Currently, I am having trouble with the Request.UrlReferrer class.

The original line is:

    [HttpPost]
    public async Task<ActionResult> ContactUsFormSubmit(ContactUs request)
    {
        var siteUrl = Request.UrlReferrer.ToString().ToLower();
        ....
    }

However, with ASP.NET Core, UrlReferrer is not available. I have found the following:

    Request.Headers["Referer"]

which returns StringValues instead of a String. I am not sure if I should try to use this one or if there is any other solutions to this situation. Request.ServerVariables is also not available or maybe I don't have the namespace. My namespaces are as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

I would really appreciate if someone can direct me in the right direction.

C# Solutions


Solution 1 - C#

You're almost there. The StringValues class is just a type ASP.NET uses to efficiently represent strings in the framework. Especially in the HttpContext object. You can just call ToString() on it to convert it to a string:

string referer = Request.Headers["Referer"].ToString();

Solution 2 - C#

As of asp.net core 2 use GetTypedHeaders

RequestHeaders header = request.GetTypedHeaders();
Uri uriReferer = header.Referer;

Solution 3 - C#

This works (tested in .NET Core 3.1):

Request.GetTypedHeaders().Referer

Request is a property of both ControllerBase (and therefore Controller too) and HttpContext, so you can get it from either.

For example, to redirect to the referring page from a controller action, just do this:

public IActionResult SomeAction()
{
    return Redirect(Request.GetTypedHeaders().Referer.ToString());
}

Solution 4 - C#

Here is how I got url referrer:-

@{
string referer = Context.Request.Headers["Referer"].ToString();
Uri baseUri = new Uri(referer);}


<form asp-action="Login" asp-route-returnUrl="@baseUri.AbsolutePath">

Solution 5 - C#

using Microsoft.AspNetCore.Server.Kestrel.Internal.Http;

var referer = ((FrameRequestHeaders)Request.Headers).HeaderReferer.FirstOrDefault();

almost the same as the accepted answer without the magic string

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKemal Tezer DilsizView Question on Stackoverflow
Solution 1 - C#Henk MollemaView Answer on Stackoverflow
Solution 2 - C#Eric HerlitzView Answer on Stackoverflow
Solution 3 - C#MarredCheeseView Answer on Stackoverflow
Solution 4 - C#Intesar AlamView Answer on Stackoverflow
Solution 5 - C#Souhaieb BesbesView Answer on Stackoverflow