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 ASP.net using C#:

    public static string BaseSiteUrl
    {
        get
        {
            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: https://www.gotknowhow.com/

 

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

    public static string BaseVirtualAppPath
    {
        get
        {
            HttpContext context = HttpContext.Current;
            string url = context.Request.ApplicationPath;
            if (url.EndsWith("/"))
                return url;
            else
                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"

 


0
8

8 Comments

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("http://forums.asp.net/t/1110512.aspx?");
string host = myUri.Host;

and more methods to get url...

http://net-informations.com/faq/asp/domain.htm

steve

Add your comment

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

Comment:
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


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 »

After running a ASP.NET website on IIS 7.5 for the first time on a Windows 7 computer, I was faced with the following error message: Login failed for user 'IIS APPPOOL\ASP.NET v4.0'. Description: An unhandled exception occurred during the execution of the...  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 »

Here's how to install Internet Information Services (IIS7) on a Windows 7 (or Vista) computer so that ASP.NET websites will run on the IIS7 web server. First, you will want to make sure that you are signed into an account with Administrator access on your...  more »

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 »

Here's how you can add "nofollow" tags to links generated by a Sitemap file that is bound to an ASP.NET Repeater control using a SiteMapDataSource. I'm currently using this technique for the footer links of GotKnowHow.com, so if you View Source of the...  more »

Here's how you can UrlEncode the plus sign (+) in a URL querystring in ASP.NET and then retrieve the plus symbol after UrlDecoding the string. In this example, I will do a postback and redirect the Server.UrlEncoded string to another page. First we will...  more »

How to implement a StopWords filter in C# that will filter out certain woulds from a query.  more »

This is one of those simple web page design things that can drive a web developer absolutely crazy.  more »

So below I'm going to share with you a fairly easy to use and understand ASP.NET User Control that allows you to pick a Date (with the ajaxToolkit CalenderExtender) and also select the Time of day using a drop down list. I've named the control...  more »

Adding a CSS border to an ASP.NET Image control was a mystery to me for the longest time. While you could easily use an html image and add the runat="server" to it and then add CSS, I really wanted to use an asp:Image control along with a CSS border....  more »

The AjaxFileUpload control that's part of the AJAX Control Toolkit, works great for easily uploading multiple files at once. However, it gets a little tricky if you want to update an UpdatePanel after all the files have finished uploading, especially if...  more »