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?
2 Answers 2
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.
-
1\$\begingroup\$ This will remove ANY port, not only 'default' 443 :( Chris's answer is much better. \$\endgroup\$Dmitry– Dmitry2016年04月28日 14:08:07 +00:00Commented 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\$tia– tia2016年05月01日 11:38:14 +00:00Commented 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\$Chris– Chris2018年04月13日 12:36:26 +00:00Commented Apr 13, 2018 at 12:36
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();
-
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– Jordan Rieger2014年11月24日 20:54:30 +00:00Commented 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\$Chris Danielson– Chris Danielson2014年12月18日 01:20:13 +00:00Commented 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\$Jordan Rieger– Jordan Rieger2014年12月18日 17:17:55 +00:00Commented Dec 18, 2014 at 17:17
https://mysitename:443/Home/Index:443
? \$\endgroup\$