148

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.

2
  • 1
    Some sites like Google wont give you the referer for security reasons. From Google eyes: Google do not want that you'll see the search text which would be a part of the referer. Commented Aug 4, 2016 at 16:12
  • FYI: URL Referer is not reliable.
    – Win
    Commented Aug 4, 2016 at 16:19

6 Answers 6

222

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();
6
  • 1
    Would you consider this to be a reliable way of doing it? Are there any alternative ways? Commented Aug 4, 2016 at 18:43
  • 5
    It is reliable in terms of accessing the property from the request headers being sent by the client. However, I wouldn't consider the value of the referrer header being send by the client reliable since it's very easy to temper. Commented Aug 4, 2016 at 18:45
  • 102
    For those who are curious if Referer is misspelled in the anwer, it is not. Although Referrer is the correct spelling, they made the misspelling in the HTTP specification english.stackexchange.com/questions/42630/referer-or-referrer/…
    – Frank Rem
    Commented Jul 3, 2017 at 9:58
  • 1
    You can find all concerning to migrations of HTTP handlers and modules to ASP.net Core in : learn.microsoft.com/en-us/aspnet/core/migration/http-modules
    – AlexGH
    Commented Oct 24, 2017 at 2:38
  • 2
    To avoid the risk of misspelling the string, use the property specifically for getting the referer: string referer = Request.Headers.Referer.ToString();
    – Guffa
    Commented Feb 11 at 12:06
84

As of asp.net core 2 use GetTypedHeaders

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

Update 2024

Since IHttpContextAccessor is more common to use the request object can be obtained like this

Inject IHttpContextAccessor as _httpContextAccessor

Fetch the referrer Uri with

private Uri GetReferrer()
{
    var header = _httpContextAccessor.HttpContext.Request.GetTypedHeaders();
    return header.Referer;
}
4
  • This must have been removed in 3
    – Snipe3000
    Commented Apr 19, 2020 at 2:17
  • 4
    Should still be in 3 @Snipe3000 learn.microsoft.com/en-us/dotnet/api/…
    – Daniel
    Commented May 7, 2020 at 22:45
  • 2
    Still there but you need to include using Microsoft.AspNetCore.Http; to get the extension method. Commented Jun 18, 2021 at 11:31
  • 2
    I like this answer because referrer here is a Uri object not a string, which is more handy to work with
    – Mojtaba
    Commented Jan 21, 2022 at 17:13
34

This works (tested in .NET Core 3.1 onward):

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());
}

Update:

Made an extension method:

using Microsoft.AspNetCore.Http;

public static class RequestExtensions
{
    public static string GetReferrer(this HttpRequest request)
    {
        return request.GetTypedHeaders().Referer.ToString();
    }
}

Usage:

public IActionResult SomeAction()
{
    return Redirect(Request.GetReferrer());
}
1
  • 3
    FYI you need to add the namespace for this - using Microsoft.AspNetCore.Http; Commented Mar 29, 2021 at 21:44
12

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">
2
  • How to access Context in razor page as you did show in that example? I have added the code but Context is undefined.
    – ZedZip
    Commented Jan 28, 2020 at 15:15
  • I'm getting it from WebViewPage class of System.Web.Mvc Commented Feb 16, 2020 at 8:47
8
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

2
  • 1
    But does require a Nuget package (Microsoft.AspNetCore.Server.Kestrel)... also has this been tested to work in IISExpress? SSL? etc.?
    – Serj Sagan
    Commented Dec 7, 2018 at 18:06
  • 1
    @SerjSagan in my oppinion the code cofidence provided by type safety (as aopposed to magic strings for the other solutions) largely outweighs the additional nuget, as for hosting, since it relies on kestrel it should work in all hosting modes. SSL was not tested but it should not make a difference Commented Dec 10, 2018 at 13:11
0

In ASP.NET Core MVC, you can get the URL referrer from the Referer header in the HTTP request. You can access this in your controller by looking at the HttpContext.Request.Headers collection

Example:

if (Request.Headers.TryGetValue(HeaderNames.Referer, out var referer))
{
  ...
}

Also, make sure to check if the referrer is empty.

Not the answer you're looking for? Browse other questions tagged or ask your own question.