9
\$\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?

Sᴀᴍ Onᴇᴌᴀ
29.6k16 gold badges45 silver badges203 bronze badges
asked Apr 25, 2013 at 10:58
\$\endgroup\$
3
  • \$\begingroup\$ What if you have URL like https://mysitename:443/Home/Index:443? \$\endgroup\$ Commented Apr 25, 2013 at 11:28
  • \$\begingroup\$ I believe Request.Url.AbsoluteUri return Url in the format mentioned above. \$\endgroup\$ Commented Apr 25, 2013 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\$ Commented Apr 25, 2013 at 12:57

2 Answers 2

15
\$\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

t3chb0t
44.7k9 gold badges84 silver badges190 bronze badges
answered Apr 25, 2013 at 11:28
\$\endgroup\$
3
  • 1
    \$\begingroup\$ This will remove ANY port, not only 'default' 443 :( Chris's answer is much better. \$\endgroup\$ Commented Apr 28, 2016 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\$ Commented May 1, 2016 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\$ Commented Apr 13, 2018 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();
answered Mar 26, 2014 at 17:41
\$\endgroup\$
3
  • 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\$ Commented Nov 24, 2014 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\$ Commented Dec 18, 2014 at 1:20
  • \$\begingroup\$ Yeah, not sure why they didn't make a const or enum for that as they usually do. \$\endgroup\$ Commented Dec 18, 2014 at 17:17

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.