There are more than 30 methods in ASP.NET classes that deal with paths and/or URLs. Many developers coming from traditional ASP tend to stick with the methods they know from ASP (which are still there), but there are many other useful methods that can save you a lot of time and headache.
Let’s take a look at everything from virtual to physical path mapping to root path reference syntax.
Some HttpRequest path and URL properties:
Request.ApplicationPath:/engineering
Request.CurrentExecutionFilePath:/engineering/Tmp/Test.aspx
Request.FilePath:/engineering/Tmp/Test.aspx
Request.Path:/engineering/Tmp/Test.aspx
Request.PathInfo:
Request.PhysicalApplicationPath:D:\Inetpub\wwwroot\brains\engineering\
Request.QueryString:/engineering/Tmp/Test.aspx?q=parameter
Request.Url.AbsolutePath:/engineering/Tmp/Test.aspx
Request.Url.AbsoluteUri:http://localhost:2036/engineering/Tmp/Test.aspx?q=parameter
Request.Url.Fragment:
Request.Url.Host:localhost
Request.Url.Authority:localhost:2036
Request.Url.LocalPath:/engineering/Tmp/Test.aspx
Request.Url.PathAndQuery:/engineering/Tmp/Test.aspx?q=parameter
Request.Url.Port:2036
Request.Url.Query:?q=parameter
Request.Url.Scheme:http
Request.Url.Segments:/engineering/Tmp/Test.aspx
Now let’s take a look at the Server.MapPath() method:
As everyone knows MapPath() converts a virtual path into a physical path.
// This will locate the Example.xml file in the App_Data folder.
string tmp = Server.MapPath("~/App_Data/Example.xml");
Performance and Optimization:
MapPath performance is over 1000 times slower than a simple string append. Therefore, it could be worthwhile to cache the paths.