Kemal Tezer Dilsiz Kemal Tezer Dilsiz - 1 year ago 1405
ASP.NET (C#) Question

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

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


The original line is:

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:


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.
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. And I hope my question follows the guidelines for the website. Thanks.

Answer Source

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();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download