2

I created several virtual directors and I want to be able to get the url from the current http request.

For example:

http://www.site.com/app_1/default.aspx ===> http://www.site.com/app_1/

http://www.site.com/app_2/default.aspx ===> http://www.site.com/app_2/

....

http://www.site.com/app_n/default.aspx ===> http://www.site.com/app_n/

My code:

    string urlApp = HttpContext.Current.Request.Url.AbsoluteUri.ToString();
    urlApp = urlApp.Substring(0, urlApp.LastIndexOf('/') + 1);

and I tried

    string urlApp = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + "/";

In localhost works great: http://localhost:2468/test.aspx result http://localhost:2468/

, but when access via virtual directory http://myhost/app_1/test.aspx result http://myhost/

how can i get http://myhost/app_1/?

Dumitru Chirutac
  • 597
  • 2
  • 7
  • 27
  • Have you tried seeing what `HttpContext.Current.Request.Url.AbsoluteUri.ToString();` actually returns? Maybe it is just returning `http://myhost/app_1/`, and then your substring is removing the app_1 part? – seekerOfKnowledge May 22 '12 at 14:04

1 Answers1

5
HttpContext.Current.Request.ApplicationPath 

is what you need to get the route to your virtual directory. Append it to the URL you've already gotten and you're onto a winner. Something like

HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpContext.Current.Request.ApplicationPath

Should work, but look out for trailing slashes.

Mel Padden
  • 913
  • 8
  • 19