22 September 2004

Get Application Path [C# / ASP.NET]

Posted by Mikhail Esteves under: C#; Tips .

public string GetApplicationPath()
  {
   string applicationPath = "";

   if (this.Page.Request.Url != null)
    applicationPath = this.Page.Request.Url.AbsoluteUri.Substring( 
     0, this.Request.Url.AbsoluteUri.ToLower().IndexOf(
      this.Request.ApplicationPath.ToLower(), 
       this.Request.Url.AbsoluteUri.ToLower().IndexOf(
      this.Page.Request.Url.Authority.ToLower()) + 
      this.Page.Request.Url.Authority.Length) + 
     this.Request.ApplicationPath.Length);
  return applicationPath;
  }

Use this in any ASP.NET page to get the application path. For example, if you run this function in http://localhost/pathtest/test.aspx, the function will return http://localhost/pathtest/

Another way to redirect is to use the .NET tilde. For example, to navigate to the home page of your website from any place, you would do Response.Redirect(”“); or even /default.aspx.



16 Comments so far...

Sanat Gersappa Says:

26 September 2004 at 11:13 pm.

If you’re using VB, you could use this function

Function GetApplicationPath()
Dim path As String = Left(Request.Url.AbsoluteUri, Request.Url.AbsoluteUri.Length + 1 – Request.Path.Length)
Return path
End Function

For redirecting, you can use /default.aspx…no ~ required.

Sanat Gersappa Says:

27 September 2004 at 12:11 am.

Ok. I was feeling sleepy when I wrote the previous comment….it doesn’t work in a subfolder :-)

Here’s something in C# that should – I’m still sleepy, so I’m quite sure it can be written more elegantly –

public string GetApplicationPath()
{
string path = String.Empty;
string[] segs = Request.Url.Segments;

for (int i = 0; i < segs.Length – 1; i++)
{
path += segs[i];
}
return Request.Url.GetLeftPart(UriPartial.Authority) + path;
}

billsmith Says:

9 December 2004 at 1:32 pm.

thank you but i think the bgcolor should bright!

mascix Says:

11 January 2005 at 5:03 pm.

I wanted to thank u this is so stupid why .NET framework doesnt have this kind of easy and usefull thing inside itself anyway thx guys

Mikhail Says:

25 January 2005 at 4:55 pm.

mascix: If you are doing just a redirect, you can use ‘~’. For example:

Response.Redirect(”~/signup/join.aspx”);

Iryx Says:

26 February 2005 at 7:29 am.


private string GetApplicationPath()
{
return HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.ApplicationPath;
}

Christian Says:

6 April 2005 at 8:49 pm.

I wrote something that uses less code

public static string GetUrlPath(System.Web.UI.Page sender)
{
string urlStr = sender.Request.RawUrl;
int pos = urlStr.LastIndexOf(”/”);
urlStr = urlStr.Substring(0,pos + 1);
return urlStr;
}

Ben Says:

19 December 2005 at 7:33 pm.

The last code block [GetUrlPath{System.Web.UI.Page sender)] worked for me while the other ones did not. I am not sure why this is the case.

Most mapped to the root of the C: drive and then attached the webserver path after that. The code posted by Sanat gave me a 404 error.

The last bit did not work by itself though. I ended up using something like this:

sXmlPath = @”\states.xml”;

XmlTextReader oXmlTxtRdr = new XmlTextReader(Server.MapPath(String.Concat(GetApplicationPath(this.Page), sXmlPath)).ToString());

Ben Says:

19 December 2005 at 7:35 pm.

Oh, sorry for the double comment, I forgot to mention that I renamed the function in the comment above mine to GetApplicationPath, and the code posted by Christian I used unchanged, I just needed some additional bits to get it to build the path for the XmlReader correctly.

Kiran Says:

9 February 2006 at 11:26 pm.

Thanks Mikhail. Works great.

Steven Says:

30 November 2006 at 10:00 pm.

I used this and it worked fine for asp.net / vb

dim path as string = Page.MapPath(”~/”) & “/App_Data/DataLists.xml”

JeyAr(MC) Says:

16 January 2007 at 12:16 am.

Left(Request.Url.AbsoluteUri,request.Url.AbsoluteUri.LastIndexOf(”/”)+1)

return the path of you current file

Ralph Says:

25 January 2007 at 3:07 am.

Have any of you considered a path such as
http://localhost/file.aspx/a/b/c/d/e/
where “file.aspx” is your actual script?

At first sight, the only one to work correctly would be Iryx’s.

Donny Says:

20 January 2008 at 7:09 pm.

string applicationUrl = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpContext.Current.Request.ApplicationPath + “/”;

Works like a charm :)

Ken Says:

2 April 2008 at 1:12 am.

This requires less code:

Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.LastIndexOf(”/”))

Allan McLemore Says:

5 April 2008 at 10:41 pm.

I’ll have to try this methodology. You can use Singleton pattern on this and persist the Application path in an object property. That way the evaluation only happens once in each application instance.

I took a slightly different approach to solving this issue. When you try to generate Urls using the various values built into ASP.NET (ApplicationPath, “~”, etc), you will encounter problems in some scenarios.

Check out my post on this issue: http://www.zenternal.com/weblog/?p=6

The code sample provided in my article does not address the issue with ASP.NET returning localhost when running on XP Workstation. However, the URL methodology in the code can be improved to use System.Environment.MachineName whenever Request.ServerVariables[“SERVER_NAME”] returns localhost. I’ll try to post an update to the article soon.

Leave a Reply

Browse

Photography

Projects

Pages

Calendar

September 2004
M T W T F S S
« Aug   Oct »
 12345
6789101112
13141516171819
20212223242526
27282930  

Categories

www.flickr.com