Let us change our traditional attitude to the construction of programs. Instead of imagining that our main task is to instruct a computer what to do, let us concentrate rather on explaining to human beings what we want a computer to do. --Donald Knuth

How Do I Get Paths and URL fragments from the HttpRequest object?

By steve on Tuesday, January 9, 2007.
Updated Saturday, January 12, 2013.
Viewed 188,597 times. (3 times today.)
DeveloperTips and TutorialsASP.NETC#

Summary

If you have done much ASP.NET programming you have probably spent a lot of time mapping urls to physical disk locations and vice versa. This need arises whenever you store files on the server or do some kind of URL parsing and re-routing of incoming requests.

This article will examine the properties of the Request object that will provide path and url information related to the application and the current request.

First, here are a couple of tables of useful properties on the Request object and an example of the text they return for a given input URL.

For some reason unclear to me, the Url.Fragment property is usually empty instead of showing "#fragment".

Input: http://localhost:96/Cambia3/Temp/Test.aspx?q=item#fragment

Some HttpRequest path and URL properties:
Request.ApplicationPath:/Cambia3
Request.CurrentExecutionFilePath:/Cambia3/Temp/Test.aspx
Request.FilePath:/Cambia3/Temp/Test.aspx
Request.Path:/Cambia3/Temp/Test.aspx
Request.PathInfo:
Request.PhysicalApplicationPath:D:\Inetpub\wwwroot\CambiaWeb\Cambia3\
Request.RawUrl:/Cambia3/Temp/Test.aspx?query=arg
Request.Url.AbsolutePath:/Cambia3/Temp/Test.aspx
Request.Url.AbsoluteUri:http://localhost:96/Cambia3/Temp/Test.aspx?query=arg
Request.Url.Fragment:
Request.Url.Host:localhost
Request.Url.Authority:localhost:96
Request.Url.LocalPath:/Cambia3/Temp/Test.aspx
Request.Url.PathAndQuery:/Cambia3/Temp/Test.aspx?query=arg
Request.Url.Port:96
Request.Url.Query:?query=arg
Request.Url.Scheme:http
Request.Url.Segments:/
Cambia3/
Temp/
Test.aspx

Input: http://localhost:96/Cambia3/Temp/Test.aspx/path/info?q=item#fragment

Some HttpRequest path and URL properties:
Request.ApplicationPath:/Cambia3
Request.CurrentExecutionFilePath:/Cambia3/Temp/Test.aspx
Request.FilePath:/Cambia3/Temp/Test.aspx
Request.Path:/Cambia3/Temp/Test.aspx/path/info
Request.PathInfo:/path/info
Request.PhysicalApplicationPath:D:\Inetpub\wwwroot\CambiaWeb\Cambia3\
Request.RawUrl:/Cambia3/Temp/Test.aspx/path/info?query=arg
Request.Url.AbsolutePath:/Cambia3/Temp/Test.aspx/path/info
Request.Url.AbsoluteUri:http://localhost:96/Cambia3/Temp/Test.aspx/path/info?query=arg
Request.Url.Fragment:
Request.Url.Host:localhost
Request.Url.LocalPath:/Cambia3/Temp/Test.aspx/path/info
Request.Url.PathAndQuery:/Cambia3/Temp/Test.aspx/path/info?query=arg
Request.Url.Port:96
Request.Url.Query:?query=arg
Request.Url.Scheme:http
Request.Url.Segments:/
Cambia3/
Temp/
Test.aspx/
path/
info

The following is the C# method I used to process the URLs above and generate the output tables. You may use this method in a code-behind file for an aspx page with a Label control names lblOutput.

private void DisplayRequestObjectProperties()
{
   lblOutput.Text = "<table cellpadding=2 border=1>";

   lblOutput.Text += "<tr><td colspan=2 align=center>";
   lblOutput.Text +=  "Some HttpRequest path and ULR properties:";
   lblOutput.Text += "</td></tr>";

   // application path
   lblOutput.Text += "<tr><td>";
   lblOutput.Text += "Request.ApplicationPath:";
   lblOutput.Text += "</td><td>";
   lblOutput.Text += "<b>" + Request.ApplicationPath + "</b>";
   lblOutput.Text += "</td></tr>";

   // current execution file path
   lblOutput.Text += "<tr><td>";
   lblOutput.Text += "Request.CurrentExecutionFilePath:";
   lblOutput.Text += "</td><td>";
   lblOutput.Text += "<b>" + Request.CurrentExecutionFilePath + "</b>";
   lblOutput.Text += "</td></tr>";

   // file path
   lblOutput.Text += "<tr><td>";
   lblOutput.Text += "Request.FilePath:";
   lblOutput.Text += "</td><td>";
   lblOutput.Text += "<b>" + Request.FilePath + "</b>";
   lblOutput.Text += "</td></tr>";

   // path
   lblOutput.Text += "<tr><td>";
   lblOutput.Text += "Request.Path:";
   lblOutput.Text += "</td><td>";
   lblOutput.Text += "<b>" + Request.Path + "</b>";
   lblOutput.Text += "</td></tr>";

   // path info
   lblOutput.Text += "<tr><td>";
   lblOutput.Text += "Request.PathInfo:";
   lblOutput.Text += "</td><td>";
   lblOutput.Text += "<b>" + Request.PathInfo + "</b>";
   lblOutput.Text += "</td></tr>";

   // physical application path
   lblOutput.Text += "<tr><td>";
   lblOutput.Text += "Request.PhysicalApplicationPath:";
   lblOutput.Text += "</td><td>";
   lblOutput.Text += "<b>" + Request.PhysicalApplicationPath + "</b>";
   lblOutput.Text += "</td></tr>";

   // raw url
   lblOutput.Text += "<tr><td>";
   lblOutput.Text += "Request.RawUrl:";
   lblOutput.Text += "</td><td>";
   lblOutput.Text += "<b>" + Request.RawUrl + "</b>";
   lblOutput.Text += "</td></tr>";

   // absolute path
   lblOutput.Text += "<tr><td>";
   lblOutput.Text += "Request.Url.AbsolutePath:";
   lblOutput.Text += "</td><td>";
   lblOutput.Text += "<b>" + Request.Url.AbsolutePath + "</b>";
   lblOutput.Text += "</td></tr>";

   // absolute uri
   lblOutput.Text += "<tr><td>";
   lblOutput.Text += "Request.Url.AbsoluteUri:";
   lblOutput.Text += "</td><td>";
   lblOutput.Text += "<b>" + Request.Url.AbsoluteUri + "</b>";
   lblOutput.Text += "</td></tr>";

   // fragment
   lblOutput.Text += "<tr><td>";
   lblOutput.Text += "Request.Url.Fragment:";
   lblOutput.Text += "</td><td>";
   lblOutput.Text += "<b>" + Request.Url.Fragment + "</b>";
   lblOutput.Text += "</td></tr>";

   // host
   lblOutput.Text += "<tr><td>";
   lblOutput.Text += "Request.Url.Host:";
   lblOutput.Text += "</td><td>";
   lblOutput.Text += "<b>" + Request.Url.Host + "</b>";
   lblOutput.Text += "</td></tr>";

   // authority
   lblOutput.Text += "<tr><td>";
   lblOutput.Text += "Request.Url.Authority:";
   lblOutput.Text += "</td><td>";
   lblOutput.Text += "<b>" + Request.Url.Authority + "</b>";
   lblOutput.Text += "</td></tr>";

   // local path
   lblOutput.Text += "<tr><td>";
   lblOutput.Text += "Request.Url.LocalPath:";
   lblOutput.Text += "</td><td>";
   lblOutput.Text += "<b>" + Request.Url.LocalPath + "</b>";
   lblOutput.Text += "</td></tr>";

   // path and query
   lblOutput.Text += "<tr><td>";
   lblOutput.Text += "Request.Url.PathAndQuery:";
   lblOutput.Text += "</td><td>";
   lblOutput.Text += "<b>" + Request.Url.PathAndQuery + "</b>";
   lblOutput.Text += "</td></tr>";

   // port
   lblOutput.Text += "<tr><td>";
   lblOutput.Text += "Request.Url.Port:";
   lblOutput.Text += "</td><td>";
   lblOutput.Text += "<b>" + Request.Url.Port + "</b>";
   lblOutput.Text += "</td></tr>";

   // query
   lblOutput.Text += "<tr><td>";
   lblOutput.Text += "Request.Url.Query:";
   lblOutput.Text += "</td><td>";
   lblOutput.Text += "<b>" + Request.Url.Query + "</b>";
   lblOutput.Text += "</td></tr>";

   // scheme
   lblOutput.Text += "<tr><td>";
   lblOutput.Text += "Request.Url.Scheme:";
   lblOutput.Text += "</td><td>";
   lblOutput.Text += "<b>" + Request.Url.Scheme + "</b>";
   lblOutput.Text += "</td></tr>";

   // segments
   lblOutput.Text += "<tr><td>";
   lblOutput.Text += "Request.Url.Segments:";
   lblOutput.Text += "</td><td>";
   string[] segments = Request.Url.Segments;
   foreach (string s in segments)
      lblOutput.Text += "<b>" + s + "</b><br>";
   lblOutput.Text += "</td></tr>";

   lblOutput.Text += "</table>";
}
Back to Top

Share This Page:


Subscribe for free and read it when and where you want!

Get the latest tips and tools in your Inbox.

Your email address will NEVER be shared and you can unsubscribed at any time using the link at the bottom of every email.

Click Here to Subscribe

(Was this post helpful? If not, please leave a comment and let me know what you would like to hear about.)


 
comments powered by Disqus
Follow:
Share This Page:


Get the latest tips and tools in your Inbox. It's 100% free.



Click Here to Subscribe
Welcome!
Welcome to my blog about software development and the Microsoft stack.

I've been a full time .NET developer for fifteen years, but I didn't start my professional life as a programmer ... more