Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2010-09-04
9
277 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:newbieweb
  • 5
  • 4
9 Comments
 
LVL 8

Accepted Solution

by:
Mohit Vijay earned 500 total points
ID: 33604374
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
 
LVL 8

Expert Comment

by:Mohit Vijay
ID: 33605438
above not worked?
0
 

Author Comment

by:newbieweb
ID: 33605470
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 8

Assisted Solution

by:Mohit Vijay
Mohit Vijay earned 500 total points
ID: 33605550
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
 
LVL 8

Assisted Solution

by:Mohit Vijay
Mohit Vijay earned 500 total points
ID: 33605552
Actually it tells that where is your file located on your server according to IIS directly setup.
0
 

Author Comment

by:newbieweb
ID: 33605560
so does that mean IIS does not allow you to have the same file in two subfolders?
0
 

Author Comment

by:newbieweb
ID: 33605562
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
 
LVL 8

Assisted Solution

by:Mohit Vijay
Mohit Vijay earned 500 total points
ID: 33605571
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
 

Author Closing Comment

by:newbieweb
ID: 33629830
Thanks.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question