user437291 user437291 - 1 month ago 4
ASP.NET (C#) Question

Can’t make sense of HttpRequest.ApplicationPath

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

Answer

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/.