What does Request.MapPath() do and how does it work?

I want to access a file that's in a folder for the default Physical Path.  So the following code works:

             String outPutPath = Path.GetDirectoryName(Request.PhysicalPath) + "\\output";
             StreamWriter w = File.AppendText(outPutPath + "\\xmleventsincall.txt");

Is there a nicer way of doing this using MapPath()?

newbiewebSr. Software EngineerAsked:
Who is Participating?
Mohit VijayConnect With a Mentor Commented:
Request.MapPath maps the specified
virtual path to a physical path. Assuming that a file named Hello.aspx
resides in C:\Inetpub\wwwroot\MyFolder, the output of

is C:\Inetpub\wwwroot\MyFolder\Hello.aspx.
Mohit VijayCommented:
above not worked?
newbiewebSr. Software EngineerAuthor Commented:
Yes, but what is keeping track of the location of "Hello.aspx"?  And what if there are two copies of that file in different directories?
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Mohit VijayConnect With a Mentor Commented:
It worked according to your setup in IIS, like

You have setup a folder ABC in issue, so you are all application will run like, www.123Hello.com/ABC/ee.aspx

If your hello.aspx is on root, i.e. in ABC folder then Response.Write(Request.MapPath("Hello.aspx"))

will give you path C:\Inetpub\wwwroot\ABC\Hello.aspx.

if you file is in any inner folder of ABC, like its also available in ABC1 and ABC2, then you have to pass

Mohit VijayConnect With a Mentor Commented:
Actually it tells that where is your file located on your server according to IIS directly setup.
newbiewebSr. Software EngineerAuthor Commented:
so does that mean IIS does not allow you to have the same file in two subfolders?
newbiewebSr. Software EngineerAuthor Commented:
In your example, Hello.aspx is located in three folders.  I don't see how it can tell you the path in that case.
Mohit VijayConnect With a Mentor Commented:
use of request.mappath is to provide the path of IIS foder, in which you are hosting your application, and after that whatever virtual path you provide in Request.MapPath, combining it with hosted application url, it will give you actual physical path.

for more information
http://dotnetperls.com/mappath-aspnet [Best example]


newbiewebSr. Software EngineerAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.