8
\$\begingroup\$

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?

\$\endgroup\$
  • \$\begingroup\$ What if you have URL like https://mysitename:443/Home/Index:443? \$\endgroup\$ – svick Apr 25 '13 at 11:28
  • \$\begingroup\$ I believe Request.Url.AbsoluteUri return Url in the format mentioned above. \$\endgroup\$ – ssilas777 Apr 25 '13 at 12:05
  • \$\begingroup\$ 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. \$\endgroup\$ – svick Apr 25 '13 at 12:57
14
\$\begingroup\$

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.

See: UriBuilder.Port Property

\$\endgroup\$
  • 1
    \$\begingroup\$ This will remove ANY port, not only 'default' 443 :( Chris's answer is much better. \$\endgroup\$ – Dmitry Apr 28 '16 at 14:08
  • \$\begingroup\$ @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. \$\endgroup\$ – tia May 1 '16 at 11:38
  • 2
    \$\begingroup\$ You can use if (uriBuilder.Uri.IsDefaultPort){uriBuilder.Port = -1;} to only get rid of it when its default. \$\endgroup\$ – Chris Apr 13 '18 at 12:36
8
\$\begingroup\$

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();
\$\endgroup\$
  • 3
    \$\begingroup\$ 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. \$\endgroup\$ – Jordan Rieger Nov 24 '14 at 20:54
  • \$\begingroup\$ 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. \$\endgroup\$ – Chris Danielson Dec 18 '14 at 1:20
  • \$\begingroup\$ Yeah, not sure why they didn't make a const or enum for that as they usually do. \$\endgroup\$ – Jordan Rieger Dec 18 '14 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.