Solved

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

Posted on 2010-09-04
9
276 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Gridview selected row 9 44
Following an example - Why do I need to click Save button twice? 3 25
VB.NET 2008 Publish Error 2 25
Linq asp.net mvc 13 6
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

776 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