How to remove the port number from a url string

前端 未结 8 565
清酒与你
清酒与你 2021-02-02 04:57

I have the following code snippet:

string tmp = String.Format(\"

        
8条回答
  •  时光说笑
    2021-02-02 05:35

    A more generic solution (works with http, https, ftp...) based on Ian Flynn idea. This method does not remove custom port, if any. Custom port is defined automatically depending on the protocol.

    var uriBuilder = new UriBuilder("http://www.google.fr/");
    if (uriBuilder.Uri.IsDefaultPort)
    {
        uriBuilder.Port = -1;
    }
    return uriBuilder.Uri.AbsoluteUri;
    

提交回复
热议问题