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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With