Home > Back-end >  How to use httpcontext on a given string URL
How to use httpcontext on a given string URL

Time:08-24

I'm trying to use httpcontext on a given string

ex:

I have this

string url = "https://subdomain.domain.com/link1/";

And I want to be able to retreive those following value from the string

Console.Write("<br/>Host "   HttpContext.Current.Request.Url.Host);
Console.Write("<br/>Authority: "   HttpContext.Current.Request.Url.Authority);
Console.Write("<br/>Port: "   HttpContext.Current.Request.Url.Port);
Console.Write("<br/>AbsolutePath: "   HttpContext.Current.Request.Url.AbsolutePath);
Console.Write("<br/>ApplicationPath: "   HttpContext.Current.Request.ApplicationPath);
Console.Write("<br/>AbsoluteUri: "   HttpContext.Current.Request.Url.AbsoluteUri);
Console.Write("<br/>PathAndQuery: "   HttpContext.Current.Request.Url.PathAndQuery);

Is there a way to do that?

CodePudding user response:

Thanks @UweKeim for the answer.

I can definitely do that by using the Uri class.

Here's the sample of code for those who need it:

Uri uri = new Uri("https://subdomain.domain.com/link1/");        

Console.WriteLine($"AbsolutePath: {uri.AbsolutePath}");
Console.WriteLine($"AbsoluteUri: {uri.AbsoluteUri}");
Console.WriteLine($"DnsSafeHost: {uri.DnsSafeHost}");
Console.WriteLine($"Fragment: {uri.Fragment}");
Console.WriteLine($"Host: {uri.Host}");
Console.WriteLine($"HostNameType: {uri.HostNameType}");
Console.WriteLine($"IdnHost: {uri.IdnHost}");
Console.WriteLine($"IsAbsoluteUri: {uri.IsAbsoluteUri}");
Console.WriteLine($"IsDefaultPort: {uri.IsDefaultPort}");
Console.WriteLine($"IsFile: {uri.IsFile}");
Console.WriteLine($"IsLoopback: {uri.IsLoopback}");
Console.WriteLine($"IsUnc: {uri.IsUnc}");
Console.WriteLine($"LocalPath: {uri.LocalPath}");
Console.WriteLine($"OriginalString: {uri.OriginalString}");
Console.WriteLine($"PathAndQuery: {uri.PathAndQuery}");
Console.WriteLine($"Port: {uri.Port}");
Console.WriteLine($"Query: {uri.Query}");
Console.WriteLine($"Scheme: {uri.Scheme}");
Console.WriteLine($"Segments: {string.Join(", ", uri.Segments)}");
Console.WriteLine($"UserEscaped: {uri.UserEscaped}");
Console.WriteLine($"UserInfo: {uri.UserInfo}");
  • Related