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(, calling GetLeftPart(UriPartial.Authority) will give you back 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!