Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 846
kimbaudi Avatar asked Sep 20 '25 05:09

kimbaudi


1 Answers

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

like image 185
stackunderflow Avatar answered Sep 22 '25 17:09

stackunderflow