ASP.NET
Topic: Getting the absolute path in ASP.NET

Ireland .NET Developer Alliance :: Forums

Page 1 of 1 (1 items)


Posted by Paschal on
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.