How to Get Base URL in ASP.NET using C#

by Updated November 20, 2011

If you do any sort of ASP.NET programming there usually comes a time when you need to get a websites Base URL.  The following shows two examples, the first example shows how to get the Base Site Url using C#, which can be used for getting both the localhost base url address and the actually domain name address when the website is live, so the code will work correctly locally and when hosted on a production environment.  The second example shows how to get the Base Virtual Application Root Path on the Server which might be used if you need to directly access files and/ or directories using the Server.MapPath() method.  

Solution for getting the Base Site Url in using C#:

    public static string BaseSiteUrl
            HttpContext context = HttpContext.Current;
            string baseUrl = context.Request.Url.Scheme + "://" + context.Request.Url.Authority + context.Request.ApplicationPath.TrimEnd('/') + '/';
            return baseUrl;

Example below shows use of "BaseSiteUrl" (For this example, the BaseSiteUrl is located in the Utilities class):

string siteLink = Utilities.BaseSiteUrl; 

The "BaseSiteUrl" will get the full base url of a website, for example:


Solution for getting the Base Virtual Application Root Path on the Server:

    public static string BaseVirtualAppPath
            HttpContext context = HttpContext.Current;
            string url = context.Request.ApplicationPath;
            if (url.EndsWith("/"))
                return url;
                return url + "/";

Example below shows use of "BaseVirtualAppPath":

string dirPath = "media/article/images/2009/01/27/";

HttpContext context = HttpContext.Current;
string dirUrl = Utilities.BaseVirtualAppPath + dirPath;
string dirFullPath = context.Server.MapPath(dirUrl);

The "BaseVirtualAppPath" will get the Base Virtual Application Root Path on the Server, for example it might return: /GotKnowHow/  if your application is named "GotKnowHow"




anonymous by Hemanat Malav on 2/20/2010
It works for me, thanks
anonymous by Zafar on 2/3/2011
Thanks u'r article save my times
anonymous by SRJ on 6/9/2011
Good one !
anonymous by HoanSangCaMau on 5/9/2012
Good for You!
Thank you very much!
anonymous by Greg on 5/30/2012
Awesome, many thanks for this, been looking for hours
anonymous by AM on 11/19/2012
Not sure only working in my local machine but when i put on server it doesn't work. May be it can upload file only when it is on local server ???? That's happen with me using other Javascript based code(s). Any help ??
anonymous by Philip on 3/19/2013
Thanks a lot!
anonymous by get URL on 4/10/2014
try this

Uri myUri = new Uri("");
string host = myUri.Host;

and more methods to get url...


Add your comment

by Anonymous - Already have an account? Login now!
Your Name:  

Enter the text you see in the image below
What do you see?
Can't read the image? View a new one.
Your comment will appear after being approved.

Related Posts

In the code below, you will be able to find the baseUrl of your website using javascript. The following javascript code will work when used on your localhost or when it's used in a live site (finds the root url of the domain address). Just add this...  more »

For a while I wasn't sure how to access GetRouteUrl from an .ashx IHttpHandler page. I wanted to access route url's setup in the Global.asax file to be used in files like rss.ashx, instead of having to hard codes the page URL's in my .ashx pages. Well,...  more »

The new Routing features in ASP.NET 4.0 are pretty awesome. However, one issue I ran into recently was trying to get the fully qualified urls from Page.GetRouteUrl as string urls to be used in emails messages. Unfortunately, I wanted something that worked...  more »