Getting the absolute path in ASP.NET
Typically when we create ASP.NET applications, we are developing against a virtual directory. However, when we deploy our app to a production web server, it is likely set up as a web site. This can have an impact on href assignments when using absolute paths. In order for paths to be consistant between virtual directories and web sites, I created a helper method that will work in either environment. Here is the code:
1: public static string GetPathFromRoot(string pathWithoutRoot)
2: {
3: string _ApplicationPath =
4: HttpContext.Current.Request.ApplicationPath;
5:
6: System.Text.StringBuilder _PathToReturn =
7: new System.Text.StringBuilder(_ApplicationPath);
8:
9: if (!pathWithoutRoot.StartsWith("/") && !_ApplicationPath.EndsWith("/"))
10: {
11: _PathToReturn.Append("/");
12: }
13:
14: _PathToReturn.Append(pathWithoutRoot);
15:
16: return _PathToReturn.ToString();
17:
18: }// GetPathFromRoot
I place this method in my global.asax.cs file.