Use the Uri.GetComponents method. To remove a port component, you will need to combine all the other components, for example:
var uri = new Uri( "http://www.example.com:80/dir/?query=test" ); var clean = uri.GetComponents( UriComponents.Scheme | UriComponents.Host | UriComponents.PathAndQuery, UriFormat.UriEscaped );
EDIT: I found a better way:
var clean = uri.GetComponents( UriComponents.AbsoluteUri & ~UriComponents.Port, UriFormat.UriEscaped );
UriComponents.AbsoluteUri saves all components, therefore & ~UriComponents.Port will exclude only the port.
Danko Durbić May 12 '10 at 2:07 pm 2010-05-12 14:07
source share