tags:

views:

34

answers:

1

HttpRequest.ApplicationPath returns virtual root path for current application, so shouldn’t in the following example when user requests http://localhost:64390/WebSite2/Default.aspx, lblApplicationPath.Text return string “/WebSite2”, since that is the path from web site’s virtual root to WebSite2 application’s virtual root? Instead it returns an empty string

Default.aspx:

<asp:Label ID="lblApplicationPath" runat="server" 
     Text='<%# HttpContext.Current.Request.ApplicationPath %>'>
</asp:Label>

Similarly an article at http://msdn.microsoft.com/en-us/library/ms178116.aspx states that with browser request to http://www.contoso.com/MyApplication/MyPages/Default.aspx, an ApplicationPath returns /, but shouldn’t it return /MyApplication, since that is the path from web site’s virtual root to application’s virtual root?

Thank you

+1  A: 

This depends on where the application is actually defined. In the cases described, it appears that the application is defined at the / level but the files sit in a subfolder. Your own example appears to use the built in web server to the studio. Perhaps you should configure your local IIS to serve this website and force the application root to be defined at some subfolder level other than http://localhost/.

Joel Etherton
So in first example folder WebSite2 is already inside the application's root level( thus it's not like the contents of WebSite2 is actually at the root level of application ) and same is true for MyApplication folder? But wouldn't article warn us if MyApplication didn't represent app's root level ( since the name of folder would make us believe otherwise)?
@user437291 - Actually the Microsoft article doesn't specify which is the case, and since that document isn't about ApplicationPath specifically, they seem to have left out the derivation. This article describes how I would expect HttpRequest.ApplicationPath to behave: http://msdn.microsoft.com/en-us/library/system.web.httprequest.applicationpath.aspx
Joel Etherton
May I also ask - since in my example application is defined at the '/' level, why doesn't HttpContext.Current.Request.ApplicationPath return '/'?
@user437291 - They are the same thing. If you have a result that produces something else, then you have a different problem. I'm assuming you mean Request.ApplicationPath because HttpRequest.ApplicationPath is invalid as an accessor.
Joel Etherton
A - "I'm assuming you mean Request.ApplicationPath because HttpRequest.ApplicationPath is invalid as an accessor" Not sure what you mean?! I used HttpContext.Current.Request.ApplicationPath in my code behind to return the path string B - "They are the same thing." Are there any methods that when called would return '/' instead of an emtpy string?