There are more than 30 methods in ASP. net classes that deal with paths and/or URLs. revoke developers coming from traditional ASP tend to stick with the methods they know from ASP (which are still there ), but there are using other useful methods that can save you a lot of time and headache. fritz onion covers the details of managing paths and URLs in ASP. net, looking at everything from virtual to physical path mapping to root path reference syntax.
A quick scan of the classes inSystem. Web .*Hierarchy reveals more than 30 methods that deal with paths and/or URLs. in this article, I will cover the more commonly used path and URL functions in ASP. net-presenting what they do, how they work, and when to use them.
The best way to understand what each of these functions and properties do is to try them out and review the results. I have prepared a page that makes callto the most commonly used path functions and properties in ASP. net, which you can download from http://staff.develop.com/onion/pathsample.zip. I recommend that you download this file, unzip it, and place the contents in a virtual directory on a machine with ASP. net installed. try to accessPathsample. aspxPage from a browser, inspect the results, and refer to your running page as you read the rest of this article.
In case you're not near a suitably equipped computer to run the page, table 1 shows the results of running the test page. this page was run and expressions were all evaluated in the context of a page calledClient. aspx, Responding to a request made to the following URL http: // localhost/informit/subdir/pathsample. aspx/extra. The physical Directory on the Web server corresponding to this virtual directory mapping wasC: \ mywebdirs \ informit \ subdir, And the top-level virtual directory for this application was calledInformitAnd was locatedC: \ mywebdirs \ informit.
Table 1 path functions in ASP. NET and their results
Expression |
Evaluation |
This. templatesourcedirectory |
/Informit/subdir |
Request. mappath ("log.txt ") |
C: \ mywebdirs \ informit \ subdir \ log.txt |
This. mappathsecure ("log.txt ") |
C: \ mywebdirs \ informit \ subdir \ log.txt |
Request. Path |
/Informit/subdir/pathsample. aspx/extra |
Request. filepath |
/Informit/subdir/pathsample. aspx |
Request. currentexecutionfilepath |
/Informit/subdir/pathsample. aspx |
Request. pathinfo |
/Extra |
Request. physicalpath |
C: \ mywebdirs \ informit \ subdir \ pathsample. aspx |
Request. physicalapplicationpath |
C: \ mywebdirs \ informit \ |
Request. applicationpath |
/Informit |
Request. url |
Http: // localhost/informit/subdir/client. aspx/extra |
Request. rawurl |
/Informit/subdir/pathsample. aspx/extra |
Response. applyapppathmodifier ("foo. aspx ") |
/Informit/subdir/Foo. aspx |
This. resolveurl ("~ /Client. aspx ") |
/Informit/pathsample. aspx |
Path Mapping
We start by looking at one of the most commonly used path methods in ASP. NET:Mappath. This useful function converts a virtual path into its corresponding physical path and is typically used to read or modify physical files on the server in the context of a Web request. It is available throughHttprequestObject, and for backward compatibility with classic ASP, it is also available throughHttpserverutilityObject (accessed viaHttpcontextOrPageServerProperty). As an example of its use, suppose you wanted to write an entry into a log file when a page was accessed. Assuming that the log file is namedLog.txtAnd is located in the same directory as the. ASPX page that is accessing it, you can write to the file with the following code:
// within a method of the Page classstring logfile = Request.MapPath("log.txt");using (StreamWriter sw = File.AppendText(logfile)){ sw.WriteLine("I was here at {0}", DateTime.Now);}
Notice in Table 1 that the callMappathWith a stringLog.txtResulted in the complete pathC: \ mywebdirs \ informit \ subdir \ log.txtCorresponding to the physical location of the virtual directory accessed in the request. In general, any time you callMappath, It prefixes the string you pass in with the complete physical directory path to the current directory to which the request was dispatched. this is useful any time you need to access a physical file on disk that is placed in the same directory as your page, special for things such as loading XML files, writing to log files, and parameter Ming file manipulation.
Be aware that any time you access the file system from within an ASP. NET application, you are operating under the restrictive rights associated with the ASPNET account (or, in Windows Server 2003, Network Service ). this account is specially created for ASP. net on your machine, and by default is granted onlyUserPrivileges. In a typical installation, this means that you cannot modify existing or create new files from within an ASP. NET page. If you find yourself wanting to useMappathTo modify or create files, the best solution is to grant the ASP. NET accountWriteAndModifyPrivileges to that one subdirectory.
There is an additional function for parameter Ming virtual to physical path mapping in ASP. NET calledMappathsecure. This method, also ofHttprequestClass, differs only in the fact that it makes a file I/O security demand as you request the translation, but it is used internally in response to the ASP. net methods. this demand is not the same as requesting whether the account has privileges to write to a file, but rather whether the code that is currently being executed has sufficient trust to perform file I/O (done through a code access security or CAS check ). most of the time, your asp. net code will be running with full privileges, so this function will always succeed. it is possible, however, in ASP. NET 1.1 to specify a lower trust level at which to run your application usingTrustElement in your configuration file (or more likely, for some third-party host environment to run your application at a lower level of trust ). if this is a possibility for you, then it may make sense to callMappathsecureInsteadMappath, As it will trigger a code access security exception sooner rather than later, giving your more flexibility in dealing with the failure.
There are two other potentially useful properties available inRequestObject:PhysicalpathAndPhysicalapplicationpath. The former is the complete physical path to the endpoint that was requested, and the latter is the complete physical path to the root of the application.