About The Author
Michael Flynn is a Web Developer at Terralever, an interactive marketing agency based in Tempe, AZ. He specializes in web technologies that include C# .NET, SQL, XML, AJAX, jQuery, Flash, and also skills in Photoshop and Illustrator. He was been involved in web development since 1998, and earned a Bachelors and Masters degree in Computer Engineering and Computer Science from the Univerisity of Louisville and holds an MSCT certificate in Web Applications.
Calendar
<<  September 2010  >>
SMTWTFS
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

Universal Root/Base Url

I have tried many ways to get the root/base url of a website.  A static approach to this is to store it in the web.config appsettings section and calling it in code.

<appSettings>
        <add key="RootUrl" value="http://www.mydotnetworld.com"/>
</appSettings>

string rootUrl = ConfigurationManager.AppSettings["RootUrl"];

This isnt convenient when you are working locally and the port number might change.  Another way is to call a method to get the base/root of the website.  I have seen numerous ways to do this and most of them had flaws.  I created my own that I use in every project and hasnt failed me yet.  It works in any development environment.

public static string RootUrl
{
    get
    {
        HttpContext context = HttpContext.Current;
        string executionPath = context.Request.ApplicationPath;
        return string.Format("{0}://{1}{2}", context.Request.Url.Scheme,
                                                                context.Request.Url.Authority,
                                                                executionPath.Length == 1 ? string.Empty : executionPath);
    }
}

I place this function inside a utility class and call it wherever the base/url is needed.  It also comes in handy within javascript.

var rooUrl = <%= Utility.RootUrl %>;

Posted on 8/16/2008 2:14:00 AM by cblaze22

Permalink | Comments (1) | Post RSSRSS comment feed |

Categories: ASP.NET | Development Tips

Tags:

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Comments

April 28. 2010 03:36

canadian payday loans

Well-written blog post. I have bookmarked it already. Have a nice day.

canadian payday loans us

Add A Comment




  Country flag