kimbaudi kimbaudi - 4 years ago 126
ASP.NET (C#) Question

How to extract scheme, host, path, and query string from the HTTP Referer using C# ASP.NET Core

How can I extract the scheme, host, path, and query string from the HTTP Referer using C#?

I'm currently working on a simple ASP.NET Core MVC 6 application and getting the HTTP Referer using

Context.Request.Headers["Referer"].ToString()
.The value of the HTTP referer is
http://localhost:5050/Blogs/Details/3
and I'm wondering how I can extract
http
for the scheme,
localhost:5050
for the host (including port), and
/Blogs/Details/3
for the path.

Is there anything in ASP.NET Core framework or C# that can do this? Or do I need to manually separate the HTTP Referer string?

Answer Source

if you save the referrer as a string say refURL = Context.Request.Headers["Referer"].ToString()

Then

var address = new System.Uri(refURL);

var scheme = address.Scheme ;
var host = address.Host;

etc

details on Uri Class

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download