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?

1

9 Answers 9

466

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.

7
  • 12
    This is the best example in my opinion. It works for localhost:port and live instances. Oct 11, 2011 at 19:13
  • 17
    GetLeftPart - really, OMG nice naming. Mar 18, 2015 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: serverfault.com/questions/757494/…
    – Dirk Boer
    Feb 16, 2016 at 11:26
  • 2
    Microsoft uses the term "Authority" for this, but I don't know if that's a standard term. See stackoverflow.com/questions/2142910/…
    – dthrasher
    Feb 17, 2016 at 2:29
  • 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. Jan 2, 2017 at 2:24
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;
4
  • 27
    Or string requested = uri.Scheme + Uri.SchemeDelimiter + uri.Authority;
    – tranmq
    Mar 1, 2012 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, 2016 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 5, 2020 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." / learn.microsoft.com/en-us/dotnet/api/…
    – Erk
    Jun 3, 2021 at 13:45
69

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. :)

2
  • 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 2, 2011 at 14:37
  • Some more details on these properties: blog.jonschneider.com/2014/10/… Jun 7, 2016 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);
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.

1
  • 1
    Great, because it works in PCL (.GetLeftPart() istn't available there)
    – Sascha
    Feb 23, 2016 at 19: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.

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

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

1
  • What does this add to Rick's accepted 2008 answer?
    – greybeard
    Oct 4, 2022 at 14:23
0

in my case i use this

Request.Url.ToString().Remove(Request.Url.ToString().Count() - (Request.ServerVariables["URL"].ToString().Count()))

where

Request.Url.ToString() 

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

Request.ServerVariables["URL"].ToString()

is return /pages/page1.aspx

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.