Web applications often need to reference absolute paths, such as to retrieve images or Xml files. Fortunately, ASP.Net provides many ways to do this using the HttpRequest class. In code-behind pages this can be referenced as a property of the page like:
|
For pages that aren't code-behinds, you can reference it through the current HttpContext like:
|
Given the following scenario, the table below shows the values for different types of paths.
- ASP.Net application "Learning_VB", which resides on localhost (located at C:\Inetpub\wwwroot).
- An aspx page, urltest, which is in the url/subfolder folder of the application
- A querystring "id=5&name=Tim"
Path Type | Value |
Page.Request.ApplicationPath | /Learning_VB |
Page.Request.CurrentExecutionFilePath | /learning_VB/url/subfolder/urltest.aspx |
Page.Request.FilePath | /learning_VB/url/subfolder/urltest.aspx |
Page.Request.Path | /learning_VB/url/subfolder/urltest.aspx |
Page.Request.PhysicalApplicationPath | C:\Inetpub\wwwroot\Learning_VB\ |
Page.Request.PhysicalPath | C:\Inetpub\wwwroot\Learning_VB\url\subfolder\urltest.aspx |
Page.Request.QueryString.ToString() | id=5&name=Tim |
Page.Request.RawUrl | /learning_VB/url/subfolder/urltest.aspx?id=5&name=Tim |
Page.Request.Url.ToString() | http://localhost/learning_VB/url/subfolder/urltest.aspx?id=5&name=Tim |
Page.Request.Url.AbsolutePath | /learning_VB/url/subfolder/urltest.aspx |
Page.Request.Url.AbsoluteUri | http://localhost/learning_VB/url/subfolder/urltest.aspx?id=5&name=Tim |
Page.Request.Url.LocalPath | /learning_VB/url/subfolder/urltest.aspx |
Page.Request.Url.PathAndQuery | /learning_VB/url/subfolder/urltest.aspx?id=5&name=Tim |
The different types of paths have several differentiators:
- Is it physical or virtual (note that web addresses use '/', and physical use '\') ?
- Does it include the querystring?
- Does it include the the full path or just start at the project folder Learning_VB)?
You can make your own version of this chart by simply creating a table on a WebForm, and then writing out the Path Type to the appropriate cell like so (note the code below is in VB, not C#):
|
I've found this a very useful chart for sorting out the different path types.
No comments:
Post a Comment