How can I get the baseurl of site? How can I get the baseurl of site? asp.net asp.net

How can I get the baseurl of site?


Try this:

string baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority +     Request.ApplicationPath.TrimEnd('/') + "/";


string baseUrl = Request.Url.GetLeftPart(UriPartial.Authority)

That's it ;)


The popular GetLeftPart solution is not supported in the PCL version of Uri, unfortunately. GetComponents is, however, so if you need portability, this should do the trick:

uri.GetComponents(    UriComponents.SchemeAndServer | UriComponents.UserInfo, UriFormat.Unescaped);