How to remove a port number from a URL string

I have the following code snippet:

string tmp = String.Format("<SCRIPT FOR='window' EVENT='onload' LANGUAGE='JavaScript'>javascript:window.open('{0}');</SCRIPT>", url); ClientScript.RegisterClientScriptBlock(this.GetType(), "NewWindow", tmp); 

The URL generated by this code includes the port number, and I think this happens because port 80 is used by the website, and in this code I am trying to load a page from the website’s virtual directory. Any ideas on how to suppress the port number in the URL string generated by this code?

+49
May 12 '10 at 13:44
source share
7 answers

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.

+101
May 12 '10 at
source share
 UriBuilder u1 = new UriBuilder( "http://www.example.com:80/dir/?query=test" ); u1.Port = -1; string clean = u1.Uri.ToString(); 

Setting the Port property to -1 on UriBuilder will remove any explicit port and implicitly use the default port value for the protocol scheme.

+52
Feb 19 '14 at 16:56
source share

A more general solution (works with http, https, ftp ...) based on the idea of ​​Ian Flynn . This method does not remove the user port, if any. The user port is determined automatically depending on the protocol.

 var uriBuilder = new UriBuilder("http://www.google.fr/"); if (uriBuilder.Uri.IsDefaultPort) { uriBuilder.Port = -1; } return uriBuilder.Uri.AbsoluteUri; 
+22
Jan 26 '16 at 10:20
source share

I would use System.Uri . I have not tried it, but it seems that ToString will really output what you want:

 var url = new Uri("http://google.com:80/asd?qwe=asdff"); var cleanUrl = url.ToString(); 

If not, you can combine the url -members components to create a cleanUrl string.

+7
May 12 '10 at 2:03 p.m.
source share
 var url = "http://google.com:80/asd?qwe=zxc#asd"; var regex = new Regex(@":\d+"); var cleanUrl = regex.Replace(url, ""); 

a solution with System.Uri also possible, but will be more bloated.

+3
May 12 '10 at 13:53
source share

You can also use the properties of URIBuilder for this. It has properties for displaying the URL as you want

+1
May 12 '10 at 14:08
source share

Ok, thanks, I figured it out ... used the KISS principle ...

 string redirectstr = String.Format( "http://localhost/Gradebook/AcademicHonestyGrid.aspx?StudentID={0}&ClassSectionId={1}&uid={2}", studid, intSectionID, HttpUtility.UrlEncode(encrypter.Encrypt(uinfo.ToXml()))); Response.Redirect(redirectstr ); 

works great for what I'm doing, which is a test harness

+1
May 13 '10 at 14:55
source share



All Articles