Monday, 3 June 2013

Get the url to the virtual root for the current request

Example(1)
Uri requestUri = Context.Request.Url;
string baseUrl = requestUri.Scheme + Uri.SchemeDelimiter + requestUri.Host + (requestUri.IsDefaultPort ? "" : ":" + requestUri.Port);


Example(2)
This can be done much easier with the following code:
string baseUrl = Request.Url.GetLeftPart(UriPartial.Authority);