I've finally learned what "upward compatible" means. It means we get to keep all our old mistakes. --Dinnie van Tassel

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

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

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>";
}
 

Version: 6.0.20200920.1535