ASP.NET path mapping: MapPath

ASP.NET has more than 30 classes that deal with path and URL. This tip looks at one of the most commonly used path methods: MapPath.

ASP.NET has more than 30 classes that deal with path and URL. If you've programmed in ASP before this, chances are you will want to stick with the classes that you're familiar with. They are still there, but there are more, and you might as well get familiar with them and use them. This tip, excerpted from InformIT, offers a discussion of one path function, MAPPATH.


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 through the HttpRequest object, and for backward compatibility with classic ASP, it is also available through the HttpServerUtility object (accessed via the HttpContext or Page Server property). 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 named log.txt and is located in the same directory as the .aspx page that is accessing it, you could write to the file with the following code:

 // within a method of the Page class string 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 call to

MapPath with a string of log.txt resulted in the complete path c:mywebdirsinformitsubdirlog.txt corresponding to the physical location of the virtual directory accessed in the request. In general, any time you call MapPath, 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, particularly for things such as loading XML files, writing to log files, and performing 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 ASP.NET account (or, in Windows Server 2003, NETWORK SERVICE). This account is specially created for ASP.NET on your machine, and by default is granted only User privileges. 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 use MapPath to modify or create files, the best solution is to grant the ASP.NET account write and modify privileges to that one subdirectory.

There is an additional function for performing virtual to physical path mapping in ASP.NET called MapPathSecure. This method, also of the HttpRequest class, 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 many of 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 using the trust element 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 call MapPathSecure instead of MapPath, 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 in the Request object: PhysicalPath and PhysicalApplicationPath. 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.


To read the entire article from which this tip comes, click over to InformIT. You don't have to register or do anything special. Just read and learn.


This was first published in October 2003

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchCloudComputing

SearchSoftwareQuality

SearchSOA

TheServerSide

SearchCloudApplications

Close