# Truncate port number from absolute Uri

We had a requirement to remove the port number from the Request.Url.AbsoluteUr i.e

Actual:

https://mysitename:443/Home/Index

Expected:

https://mysitename/Home/Index

The code I used for this is

 string newUrl = context.Request.Url.AbsoluteUri.Replace(":" + context.Request.Url.Port, string.Empty);


This is working fine. but I'm eager to know is there any better way to do this?

• What if you have URL like https://mysitename:443/Home/Index:443? – svick Apr 25 '13 at 11:28
• I believe Request.Url.AbsoluteUri return Url in the format mentioned above. – ssilas777 Apr 25 '13 at 12:05
• It does, but if you have an URL where the port part is by chance repeated in the body of the URL, your code won't work correctly. – svick Apr 25 '13 at 12:57

Use UriBuilder and set Port to -1

Uri oldUri = new Uri("http://myhost:443/Home/Index");
UriBuilder builder = new UriBuilder(oldUri);
builder.Port = -1;
Uri newUri = builder.Uri;


In case you want to remove the port part only when it is default, e.g. 80 for http and 443 for https, use snippet below (credit goes to Chris for the idea)

static Uri RemovePortIfDefault(Uri uri) {
if (uri.IsDefaultPort && uri.Port != -1) {
UriBuilder builder = new UriBuilder(uri);
builder.Port = -1;
return builder.Uri;
}
else return uri;
}


If the Port property is set to a value of -1, this indicates that the default port value for the protocol scheme will be used to connect to the host.

• This will remove ANY port, not only 'default' 443 :( Chris's answer is much better. – Dmitry Apr 28 '16 at 14:08
• @Dmitry Yes, that might be the case but also depends on what you need. If you follow the OP code, so my code just do exactly the same as OP code does, which is removing ANY port. However, if you want to just remove port number when it is the standard port number, so Chris answer is the correct one, though I cannot find anywhere in the document that it will always omit standard port number. – tia May 1 '16 at 11:38
• You can use if (uriBuilder.Uri.IsDefaultPort){uriBuilder.Port = -1;} to only get rid of it when its default. – Chris Apr 13 '18 at 12:36

I have witnessed the UriBuilder .port = -1 fix elsewhere. Frankly guys, it is a hack that really isn't a good production ready solution.

The right solution here is to use UriBuilder as follows.

var uri = new UriBuilder(baseUri);
string newUrl = uri.Uri.ToString();

• I disagree. Setting the port to -1 to suppress it is well documented in the framework and should be considered production-ready. See MSDN: msdn.microsoft.com/en-us/library/…. Also, if you are passing a UriBuilder object around, the eventual user of it may want to call .ToString() on it rather than .Uri.ToString(), and that would again include the redundant port information. – Jordan Rieger Nov 24 '14 at 20:54
• Agreed as well. The documentation does show it to be. Just wonder why on earth they wouldn't use a CONST value for that thing. – Chris Danielson Dec 18 '14 at 1:20
• Yeah, not sure why they didn't make a const or enum for that as they usually do. – Jordan Rieger Dec 18 '14 at 17:17