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()?

Thanks,
newbieweb
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
Response.Write(Request.MapPath("Hello.aspx"))

is C:\Inetpub\wwwroot\MyFolder\Hello.aspx.
0
 
Mohit VijayCommented:
above not worked?
0
 
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?
0
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


Response.Write(Request.MapPath("~/ABC1/Hello.aspx"))
Response.Write(Request.MapPath("~/ABC2/Hello.aspx"))
0
 
Mohit VijayConnect With a Mentor Commented:
Actually it tells that where is your file located on your server according to IIS directly setup.
0
 
newbiewebSr. Software EngineerAuthor Commented:
so does that mean IIS does not allow you to have the same file in two subfolders?
0
 
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.
0
 
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]

http://msdn.microsoft.com/en-us/library/0e7ykf56.aspx
http://authors.aspalliance.com/aspxtreme/sys/web/httprequestclassmappath.aspx

0
 
newbiewebSr. Software EngineerAuthor Commented:
Thanks.
0
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.