Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Help Understanding Server.MapPath Better

Status
Not open for further replies.

soho34

IS-IT--Management
Dec 28, 2004
102
US
Hi,

I have used ASP off and on for a few years, but now I'm really hunkering down to understand its mechanics.

I'm having trouble understanding why to use Server.MapPath.

I have all of the "textbook" defintions and in general it does make sense that you would need to map a virutal path to a physical directory (so you know where to look for files).

Here's a couple of the definitions I have:

The Server.MapPath method can be used to map a virtual path to the physical directory on the server. This method is mostly used when you are working with files. It does not check whether the file that, you provided exists or not.

Ok. Another definition is "server.mappath is used to map a virtual path to the physical directory on the server".

But these definitions still do not tell me WHY to use server.mappath.

I did the following to test it out:
Code:
<%
	response.write server.mappath("inc1mf.asp")
%>

This file doesn't exist.

The results I got were:

c:\inetpub\ (I ran this template containing this code in the root dir.)

I have a couple of problems with this:

This file doesn't exist in any of my root directories (I know mappath doesn't tell you whether or not the file exists).

But is this a better explanation of server.mappath (in layman's terms)? Server.MapPath simply specifies a physical directory for a filename you're intending to use. It explicitly states where to look for files.

Because in essence, aren't you just really saying "here's the path to use", and doesn't that path "default" to your root directory (or whatever directory your present template is in)? or in my case c:\inetpub\ Unless you specify otherwise?

Thanks to anyone who can provide more insight or examples for the usage of Server.MapPath.

Thanks in advance,
soho34
 
One handy use of Server.MapPath is to programmatically determine where the current ASP script is executing.

Two examples:
* Suppose your development environment has a different folder structure than your production envirnonment. Lets say your web site is under C:\Inetpub on your local box but it is under E:\Data\Inetpub in production. Now suppose you need to use an ADO Connection String to an MS Access .mdb file. You can use MapPath to figure out where your script is running and adjust the Connection String accordingly... A modification of this scenario is if you need to use FSO to programatically read or write some other file.

* Suppose you have a large piece of code that is repeated in several places in your web application. You cut and paste it into various ASP files on your site and then change the file paths by hand... hard coding them. Now lets say that sometimes you forget and it breaks and it is annoying... so you change the code to use MapPath to figure out where it is currently running and then adjust the path string programmically.
 
But is this a better explanation of server.mappath (in layman's terms)? Server.MapPath simply specifies a physical directory for a filename you're intending to use. It explicitly states where to look for files.
Yes.

Because in essence, aren't you just really saying "here's the path to use", and doesn't that path "default" to your root directory (or whatever directory your present template is in)? or in my case c:\inetpub\ Unless you specify otherwise?

Yes

If you specify just a filename it will add the physical path starting at the root of the drive and the folder that IIS is set to use as the storage folders for all the sites.

so if you have not changed the default location it will be c:\inetpub\ But if your files are located elsewhere on the server it will be

drive_letter:\path_to_files\filename.ext

Chris.

Indifference will be the downfall of mankind, but who cares?
Woo Hoo! the cobblers kids get new shoes.
People Counting Systems

So long, and thanks for all the fish.
 
Another reason for using Server.Mappath is if your application get's moved to another server or your server goes down and the backup server comes online your app would not work well with a fixed path hardcoded into it.

And if you have multiple web servers with load balancing then you do not know which server at any given time will be executing the code and a fixed path would force it to look at a specific server rather than the one the load balancing has passed the request to.
We have 4 servers on our company intranet with load balancing. Occasionally the application gets out of sync on one of the 4 servers and is executing a different version of the code. Some clients might complain of errors or other odd things happening but others are saying it works fine. :)



Paranoid? ME?? WHO WANTS TO KNOW????
 
Additionally, Server.MapPath allows you to place code in an include file that can be used from multiple locations, rather than hardcoding a file path and restricting that code to only be executed from one place.
Returning file paths that do not exist can be useful. Say you have a caching system set up that caches output to a file. First step would be to check if the file exists (build the path with MapPath, fso.FileExists), then to write or create it as needed.
Say your saving a file that has been uploaded from a form, generally you would want to use a MapPath to create the file at the correct location. This allows you to use a relative location and still pass the fso.CreateFile a fully qualified path.

barcode_1.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top