Jeremy Jeremy - 1 year ago 132
ASP.NET (C#) Question

.NET - Get protocol, host, and port

Is there a simple way in .NET to quickly get the current protocol, host, and port? For example, if I'm on the following URL:

I need to return:

I know I can use
to get the complete URL, and I know I can use
to get the host and port, but I'm not sure of the best way to get the protocol without parsing out the URL string.

Any suggestions?

Answer Source

The following (C#) code should do the trick

Uri uri = new Uri("");
string requested = uri.Scheme + Uri.SchemeDelimiter + uri.Host + ":" + uri.Port;