Wednesday, March 16, 2005

Understanding HttpRequest URLs

[This was originally posted at http://timstall.dotnetdevelopersjournal.com/understanding_httprequest_urls.htm]

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:

Page.Request.PhysicalApplicationPath()

For pages that aren't code-behinds, you can reference it through the current HttpContext like:

System.Web.HttpContext.Current.Request.PhysicalApplicationPath()

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 TypeValue
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.PhysicalApplicationPathC:\Inetpub\wwwroot\Learning_VB\
Page.Request.PhysicalPathC:\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.AbsoluteUrihttp://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#):


    Private Sub Page_Load(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles MyBase.Load

        Me.Label1.Text = Page.Request.ApplicationPath
        Me.Label2.Text = Page.Request.CurrentExecutionFilePath
        Me.Label3.Text = Page.Request.FilePath
        Me.Label4.Text = Page.Request.Path
        Me.Label5.Text = Page.Request.PhysicalApplicationPath
        Me.Label6.Text = Page.Request.PhysicalPath
        Me.Label7.Text = Page.Request.QueryString.ToString()
        Me.Label8.Text = Page.Request.RawUrl
        Me.Label9.Text = Page.Request.Url.ToString()
        Me.Label10.Text = Page.Request.Url.AbsolutePath
        Me.Label11.Text = Page.Request.Url.AbsoluteUri
        Me.Label12.Text = Page.Request.Url.LocalPath
        Me.Label13.Text = Page.Request.Url.PathAndQuery

    End Sub

I've found this a very useful chart for sorting out the different path types.

No comments:

Post a Comment