Little wonders: Uri.GetLeftPart
File another one under learning something new everyday. Patrick was asking me what the best way is to get the scheme + host + port of a Uri in .NET. We discussed a few ways that we’ve done it in the past, but agreed that there must be a better way.
Behold, Uri.GetLeftPart
! This will give you everything up to and including the part you want.
Given a new Uri(http://somewhere.com:8080/this/is/path?andQuery#fragment)
, calling GetLeftPart(UriPartial.Authority)
will give you back http://somewhere.com:8080
. You can also use Scheme
, Path
, and Query
. There’s a table showing what comes back at each part in the MSDN documentation.
It sure beats the string manipulations I’ve done in the past!
Read other posts