Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove the port number from a url string

Tags:

c#

asp.net

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 include the port number and I think that is happening because port 80 is used by the website and in this code I am trying to load a page from a virtual directory of the website. Any ideas on how to suppress the port number in the URL string generated by this code?

like image 390
Brono The Vibrator Avatar asked May 12 '10 13:05

Brono The Vibrator


People also ask

How to remove the port number from URL in asp net?

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

How to remove port number from URL in JavaScript?

I have found best solution here. var url = 'http://localhost:7001/www.facebook.com'; // Create a regex to match protocol, domain, and host var matchProtocolDomainHost = /^. *\/\/[^\/]+:?[0-9]? \//i; // Replace protocol, domain and host from url, assign to myNewUrl var myNewUrl = url.


7 Answers

Use the Uri.GetComponents method. To remove the port component you'll have to combine all the other components, something like:

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've found a better way:

var clean = uri.GetComponents( UriComponents.AbsoluteUri & ~UriComponents.Port,
                               UriFormat.UriEscaped );

UriComponents.AbsoluteUri preservers all the components, so & ~UriComponents.Port will only exclude the port.

like image 117
Danko Durbić Avatar answered Oct 05 '22 09:10

Danko Durbić


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.

like image 42
Ian Flynn Avatar answered Oct 05 '22 09:10

Ian Flynn


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;

April 2021 update

With newer .NET versions, Uri.AbsoluteUri removes the default ports and retains the custom port by default. The above code-snippet is equivalent to:

    var uriBuilder = new UriBuilder("http://www.google.fr/");
    return uriBuilder.Uri.AbsoluteUri;
like image 24
labilbe Avatar answered Oct 05 '22 08:10

labilbe


I would use the System.Uri for this. I have not tried, but it seems it's ToString will actually 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 components of the url-members to create your cleanUrl string.

like image 38
Isak Savo Avatar answered Oct 05 '22 09:10

Isak Savo


var url = "http://google.com:80/asd?qwe=zxc#asd";
var regex = new Regex(@":\d+");
var cleanUrl = regex.Replace(url, "");

the solution with System.Uri is also possible but will be more bloated.

like image 25
zerkms Avatar answered Oct 05 '22 08:10

zerkms


You can use the UriBuilder and set the value of the port to -1

and the code will be like this:

Uri tmpUri = new Uri("http://LocalHost:443/Account/Index");
UriBuilder builder = new UriBuilder(tmpUri);
builder.Port = -1;
Uri newUri = builder.Uri;
like image 41
khaled saleh Avatar answered Oct 05 '22 08:10

khaled saleh


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 fine for what I am doing which is a test harness

like image 26
Brono The Vibrator Avatar answered Oct 05 '22 08:10

Brono The Vibrator