<p>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:</p>

<p><code>http://www.mywebsite.com:80/pages/page1.aspx</code></p>

<p>I need to return:</p>

<p><code>http://www.mywebsite.com:80</code></p>

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

<p>Any suggestions?</p>

[{'answer_id': 21653, 'author': 'Dale Ragan', 'author_id': 1117, 'author_profile': 'https://Stackoverflow.com/users/1117', 'pm_score': 5, 'selected': False, 'text': '<p>Request.Url will return you the <a href="http://msdn.microsoft.com/en-us/library/system.uri.aspx" rel="noreferrer">Uri</a> of the request.  Once you have that, you can retrieve pretty much anything you want.  To get the protocol, call the <a href="http://msdn.microsoft.com/en-us/library/system.uri.scheme.aspx" rel="noreferrer">Scheme</a> property.</p>\n\n<p>Sample:</p>\n\n<pre><code>Uri url = Request.Url;\nstring protocol = url.Scheme;\n</code></pre>\n\n<p>Hope this helps.</p>\n'}, {'answer_id': 22361, 'author': 'Rick', 'author_id': 4367, 'author_profile': 'https://Stackoverflow.com/users/4367', 'pm_score': 9, 'selected': True, 'text': '<p>The following (C#) code should do the trick</p>\n\n<pre><code>Uri uri = new Uri("http://www.mywebsite.com:80/pages/page1.aspx");\nstring requested = uri.Scheme + Uri.SchemeDelimiter + uri.Host + ":" + uri.Port;\n</code></pre>\n'}, {'answer_id': 578083, 'author': 'dthrasher', 'author_id': 19112, 'author_profile': 'https://Stackoverflow.com/users/19112', 'pm_score': 9, 'selected': False, 'text': '<p>Even though @Rick has the accepted answer for this question, there\'s actually a shorter way to do this, using the poorly named <a href="https://learn.microsoft.com/en-us/dotnet/api/system.uri.getleftpart" rel="noreferrer"><code>Uri.GetLeftPart()</code></a> method.</p>\n<pre><code>Uri url = new Uri(&quot;http://www.mywebsite.com:80/pages/page1.aspx&quot;);\nstring output = url.GetLeftPart(UriPartial.Authority);\n</code></pre>\n<p>There is one catch to <code>GetLeftPart()</code>, 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 <code>GetLeftPart()</code> in my example above will be <code>http://www.mywebsite.com</code>.</p>\n<p>If the port number had been something other than 80, it would be included in the result.</p>\n'}, {'answer_id': 4195319, 'author': 'Holger', 'author_id': 317431, 'author_profile': 'https://Stackoverflow.com/users/317431', 'pm_score': 6, 'selected': False, 'text': "<p>Well if you are doing this in Asp.Net or have access to HttpContext.Current.Request\nI'd say these are easier and more general ways of getting them:</p>\n\n<pre><code>var scheme = Request.Url.Scheme; // will get http, https, etc.\nvar host = Request.Url.Host; // will get www.mywebsite.com\nvar port = Request.Url.Port; // will get the port\nvar path = Request.Url.AbsolutePath; // should get the /pages/page1.aspx part, can't remember if it only get pages/page1.aspx\n</code></pre>\n\n<p>I hope this helps. :)</p>\n"}, {'answer_id': 5238832, 'author': 'Haonan Tan', 'author_id': 650550, 'author_profile': 'https://Stackoverflow.com/users/650550', 'pm_score': 5, 'selected': False, 'text': '<p>A more structured way to get this is to use UriBuilder. This avoids direct string manipulation. </p>\n\n<pre><code>var builder = new UriBuilder(Request.Url.Scheme, Request.Url.Host, Request.Url.Port);\n</code></pre>\n'}, {'answer_id': 32615682, 'author': 'Mark Shapiro', 'author_id': 789680, 'author_profile': 'https://Stackoverflow.com/users/789680', 'pm_score': 5, 'selected': False, 'text': '<p>Even shorter way, may require newer ASP.Net:</p>\n\n<pre class="lang-cs prettyprint-override"><code>string authority = Request.Url.GetComponents(UriComponents.SchemeAndServer,UriFormat.Unescaped)\n</code></pre>\n\n<p>The UriComponents enum lets you specify which component(s) of the URI you want to include.</p>\n'}, {'answer_id': 33714244, 'author': 'benscabbia', 'author_id': 3828228, 'author_profile': 'https://Stackoverflow.com/users/3828228', 'pm_score': 4, 'selected': False, 'text': '<p>Very similar to Holger\'s answer. If you need to grab the URL can do something like: </p>\n\n<pre><code>Uri uri = Context.Request.Url;         \nvar scheme = uri.Scheme // returns http, https\nvar scheme2 = uri.Scheme + Uri.SchemeDelimiter; // returns http://, https://\nvar host = uri.Host; // return www.mywebsite.com\nvar port = uri.Port; // returns port number\n</code></pre>\n\n<p>The <a href="https://msdn.microsoft.com/en-us/library/system.uri(v=vs.110).aspx" rel="noreferrer">Uri class</a> provides a whole range of methods, many which I have not listed. </p>\n\n<p>In my instance, I needed to grab <code>LocalHost</code> along with the <code>Port Number</code>, so this is what I did: </p>\n\n<pre><code>var Uri uri = Context.Request.Url;\nvar host = uri.Scheme + Uri.SchemeDelimiter + uri.Host + ":" + uri.Port; \n</code></pre>\n\n<p>Which successfully grabbed: <code>http://localhost:12345</code></p>\n'}, {'answer_id': 73877202, 'author': 'Kashif Usman I', 'author_id': 15583595, 'author_profile': 'https://Stackoverflow.com/users/15583595', 'pm_score': -1, 'selected': False, 'text': '<p>In my case</p>\n<pre><code>Uri uri = new Uri(&quot;http://www.mywebsite.com:80/pages/page1.aspx&quot;);\nstring requested = uri.Scheme + Uri.SchemeDelimiter + uri.Authority;\n</code></pre>\n<p>works to get</p>\n<p><a href="https://www.mywebsite.com:80" rel="nofollow noreferrer">https://www.mywebsite.com:80</a></p>\n'}]
['https://Stackoverflow.com/questions/21640', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2076253/']