Solved

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

Posted on 2010-09-04
9
281 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
[X]
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
  • 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

710 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