278

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:

http://www.mywebsite.com:80/pages/page1.aspx

I need to return:

http://www.mywebsite.com:80

I know I can use Request.Url.AbsoluteUri to get the complete URL, and I know I can use Request.Url.Authority 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?

Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
Jeremy
  • 3,833
  • 2
  • 22
  • 13

8 Answers8

464

Even though @Rick has the accepted answer for this question, there's actually a shorter way to do this, using the poorly named Uri.GetLeftPart() method.

Uri url = new Uri("http://www.mywebsite.com:80/pages/page1.aspx");
string output = url.GetLeftPart(UriPartial.Authority);

There is one catch to GetLeftPart(), however. If the port is the default port for the scheme, it will strip it out. Since port 80 is the default port for http, the output of GetLeftPart() in my example above will be http://www.mywebsite.com.

If the port number had been something other than 80, it would be included in the result.

Luke Vo
  • 17,859
  • 21
  • 105
  • 181
dthrasher
  • 40,656
  • 34
  • 113
  • 139
  • 12
    This is the best example in my opinion. It works for localhost:port and live instances. – The Muffin Man Oct 11 '11 at 19:13
  • 17
    GetLeftPart - really, OMG nice naming. – Valentin Kuzub Mar 18 '15 at 14:36
  • Does anyone know if this part of the url has a common used name? In JavaScript it's called Origin, but I'm not sure if that's universally agreed on: http://serverfault.com/questions/757494/is-origin-the-common-agreed-name-for-protocol-hostname-port – Dirk Boer Feb 16 '16 at 11:26
  • 2
    Microsoft uses the term "Authority" for this, but I don't know if that's a standard term. See http://stackoverflow.com/questions/2142910/whats-the-difference-between-uri-host-and-uri-authority – dthrasher Feb 17 '16 at 02:29
  • GetLeftPart probably best solution for initial problem, just be careful source of the Uri, if its ASP .Net Request object it may have overridden URL in cluster environment. In my case it was missing port after deployment. Solution can be extension method from [FunnelWeb source code](https://bitbucket.org/TheBlueSky/funnelweb/src/b64c74f361d3/src/FunnelWeb/Utilities/HttpRequestExtensions.cs) – Grigorii Aug 03 '16 at 13:55
  • 2
    I personally do not like using the `Uri` class this way. There are a multitude of cases where this constructor will throw an exception. For most cases, I prefer my code to be a little more forgiving before aborting the task at hand. – Jonathan Wood Jan 02 '17 at 02:24
  • to get the rightParts(that's what i think :) ) `var query = url.PathAndQuery;` – Kugan Kumar May 25 '18 at 06:44
187

The following (C#) code should do the trick

Uri uri = new Uri("http://www.mywebsite.com:80/pages/page1.aspx");
string requested = uri.Scheme + Uri.SchemeDelimiter + uri.Host + ":" + uri.Port;
NobodyNada
  • 7,529
  • 6
  • 44
  • 51
Rick
  • 3,361
  • 1
  • 22
  • 29
  • 27
    Or string requested = uri.Scheme + Uri.SchemeDelimiter + uri.Authority; – tranmq Mar 01 '12 at 22:51
  • 21
    This is arguably the wrong answer as it will insert a ":" even when there is no port. Use `Uri.GetLeftPart` as @dthrasher points out – gman Jan 28 '16 at 10:49
  • Note that in some cases, you still want to concatenate with uri.Headers["Host"] instead of GetLeftPart(), e.g. behind a proxy your service may be listening on a different/non-standard port and if you use that url in a callback (with the private port) the host will be unreachable. – doveryai Jun 05 '20 at 19:56
  • There is no need to worry about not having a port number with this solution. Microsoft says "If a port is not specified as part of the URI, the Port property returns the default value for the protocol." / https://learn.microsoft.com/en-us/dotnet/api/system.uri.port?view=net-5.0 – Erk Jun 03 '21 at 13:45
68

Well if you are doing this in Asp.Net or have access to HttpContext.Current.Request I'd say these are easier and more general ways of getting them:

var scheme = Request.Url.Scheme; // will get http, https, etc.
var host = Request.Url.Host; // will get www.mywebsite.com
var port = Request.Url.Port; // will get the port
var path = Request.Url.AbsolutePath; // should get the /pages/page1.aspx part, can't remember if it only get pages/page1.aspx

I hope this helps. :)

Holger
  • 2,243
  • 17
  • 19
  • 1
    I prefer this method, I can get just the peice I want and don't have to worry about whether or not the string is well formed enough to get the port. +1 – Jrud Nov 02 '11 at 14:37
  • Some more details on these properties: http://blog.jonschneider.com/2014/10/systemuri-net-framework-45-visual-guide.html – Jon Schneider Jun 07 '16 at 19:25
38

A more structured way to get this is to use UriBuilder. This avoids direct string manipulation.

var builder = new UriBuilder(Request.Url.Scheme, Request.Url.Host, Request.Url.Port);
Haonan Tan
  • 381
  • 3
  • 2
24

Even shorter way, may require newer ASP.Net:

string authority = Request.Url.GetComponents(UriComponents.SchemeAndServer,UriFormat.Unescaped)

The UriComponents enum lets you specify which component(s) of the URI you want to include.

HadiRj
  • 1,015
  • 3
  • 21
  • 41
Mark Shapiro
  • 1,192
  • 10
  • 13
23

Request.Url will return you the Uri of the request. Once you have that, you can retrieve pretty much anything you want. To get the protocol, call the Scheme property.

Sample:

Uri url = Request.Url;
string protocol = url.Scheme;

Hope this helps.

Dale Ragan
  • 18,202
  • 3
  • 54
  • 70
13

Very similar to Holger's answer. If you need to grab the URL can do something like:

Uri uri = Context.Request.Url;         
var scheme = uri.Scheme // returns http, https
var scheme2 = uri.Scheme + Uri.SchemeDelimiter; // returns http://, https://
var host = uri.Host; // return www.mywebsite.com
var port = uri.Port; // returns port number

The Uri class provides a whole range of methods, many which I have not listed.

In my instance, I needed to grab LocalHost along with the Port Number, so this is what I did:

var Uri uri = Context.Request.Url;
var host = uri.Scheme + Uri.SchemeDelimiter + uri.Host + ":" + uri.Port; 

Which successfully grabbed: http://localhost:12345

benscabbia
  • 17,592
  • 13
  • 51
  • 62
0

In my case

Uri uri = new Uri("http://www.mywebsite.com:80/pages/page1.aspx");
string requested = uri.Scheme + Uri.SchemeDelimiter + uri.Authority;

works to get

https://www.mywebsite.com:80

Nick Vu
  • 14,512
  • 4
  • 21
  • 31