I recently needed to generate urls in my codebehind that were full urls to the site. This required getting the url to the root of the site so I could then add the directory/file to that root. The following does, I believe, handle all cases. It has passed all of our tests so far:
string baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd('/') + '/';
Do you find this useful? If so please check out Windward Reports.
Or Request.Url.GetLeftPart(UriPartial.Authority).
Posted by: Chris M | July 27, 2011 at 03:31 AM
httpcontext.current
this is available everywhere since its static itself
Posted by: John | April 03, 2009 at 03:31 AM
Ok, now how do you do that when you don't have a HttpRequest. Like say in the constructor or a static method. There has to be a way, this is silly.
Posted by: Jordan | June 04, 2008 at 09:26 AM
Awesome! Thanks!
Posted by: Tim Wilson | February 23, 2008 at 10:29 AM
useful, 10x
Posted by: proka | December 13, 2007 at 03:58 AM