473,382 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Server.MapPath & Request.MapPath

Server.MapPath returns the physical file path that corresponds to the
specified virtual path whereas 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 both

Response.Write(Server.MapPath("Hello.aspx"))

&

Response.Write(Request.MapPath("Hello.aspx"))

is C:\Inetpub\wwwroot\MyFolder\Hello.aspx. So what's the difference
between Server.MapPath & Request.MapPath?

Thanks

Oct 6 '07 #1
3 15031
Hello rn**@rediffmail.com,

There are no difference in this aspect, because the Server.MapPath calls the
_context.Request.MapPath(path)
inside its method

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo

Server.MapPath returns the physical file path that corresponds to the
specified virtual path whereas 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 both

Response.Write(Server.MapPath("Hello.aspx"))

&

Response.Write(Request.MapPath("Hello.aspx"))

is C:\Inetpub\wwwroot\MyFolder\Hello.aspx. So what's the difference
between Server.MapPath & Request.MapPath?

Thanks

Oct 6 '07 #2
The way I look at it, there's a single MapPath method
which is called in different contexts with different parameters.

public virtual string MapPath(string virtualPath)
{
return null;
}

public string MapPath(string virtualPath)
{
return this.MapPath(VirtualPath.CreateAllowNull(virtualPa th));
}

internal string MapPath(VirtualPath virtualPath)
{
if (this._wr != null)
{
return this.MapPath(virtualPath, this.FilePathObject, true);
}
return virtualPath.MapPath();
}

public string MapPath(string virtualPath, string baseVirtualDir, bool allowCrossAppMapping)
{
VirtualPath filePathObject;
if (string.IsNullOrEmpty(baseVirtualDir))
{
filePathObject = this.FilePathObject;
}
else
{
filePathObject = VirtualPath.CreateTrailingSlash(baseVirtualDir);
}
return this.MapPath(VirtualPath.CreateAllowNull(virtualPa th), filePathObject, allowCrossAppMapping);
}

internal string MapPath(VirtualPath virtualPath, VirtualPath baseVirtualDir, bool allowCrossAppMapping)
{
if (this._wr == null)
{
throw new HttpException(SR.GetString("Cannot_map_path_withou t_context"));
}
if (virtualPath == null)
{
virtualPath = VirtualPath.Create(".");
}
VirtualPath path = virtualPath;
if (baseVirtualDir != null)
{
virtualPath = baseVirtualDir.Combine(virtualPath);
}
if (!allowCrossAppMapping)
{
virtualPath.FailIfNotWithinAppRoot();
}
string str = virtualPath.MapPathInternal();
if (((virtualPath.VirtualPathString == "/") && (path.VirtualPathString != "/"))
&& (!path.HasTrailingSlash && UrlPath.PathEndsWithExtraSlash(str)))
{
str = str.Substring(0, str.Length - 1);
}
InternalSecurityPermissions.PathDiscovery(str).Dem and();
return str;
}

public string MapPath(string path)
{
if (this._context == null)
{
throw new HttpException(SR.GetString("Server_not_available") );
}
return this._context.Request.MapPath(path);
}

public string MapPath()
{
return HostingEnvironment.MapPath(this);
}

Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espanol : http://asp.net.do/foros/
======================================
"Michael Nemtsev" <ne*****@msn.comwrote in message news:3d*************************@msnews.microsoft. com...
Hello rn**@rediffmail.com,

There are no difference in this aspect, because the Server.MapPath calls the _context.Request.MapPath(path)
inside its method

---
WBR, Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour
"The greatest danger for most of us is not that our aim is too high and we miss it, but that it is too low and we
reach it" (c) Michelangelo
>Server.MapPath returns the physical file path that corresponds to the
specified virtual path whereas 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 both

Response.Write(Server.MapPath("Hello.aspx"))

&

Response.Write(Request.MapPath("Hello.aspx"))

is C:\Inetpub\wwwroot\MyFolder\Hello.aspx. So what's the difference
between Server.MapPath & Request.MapPath?

Thanks



Oct 6 '07 #3
On Oct 6, 6:45 am, "Juan T. Llibre" <nomailrepl...@nowhere.comwrote:
The way I look at it, there's a single MapPath method
which is called in different contexts with different parameters.

public virtual string MapPath(string virtualPath)
{
return null;

}

public string MapPath(string virtualPath)
{
return this.MapPath(VirtualPath.CreateAllowNull(virtualPa th));

}

internal string MapPath(VirtualPath virtualPath)
{
if (this._wr != null)
{
return this.MapPath(virtualPath, this.FilePathObject, true);
}
return virtualPath.MapPath();

}

public string MapPath(string virtualPath, string baseVirtualDir, bool allowCrossAppMapping)
{
VirtualPath filePathObject;
if (string.IsNullOrEmpty(baseVirtualDir))
{
filePathObject = this.FilePathObject;
}
else
{
filePathObject = VirtualPath.CreateTrailingSlash(baseVirtualDir);
}
return this.MapPath(VirtualPath.CreateAllowNull(virtualPa th), filePathObject, allowCrossAppMapping);

}

internal string MapPath(VirtualPath virtualPath, VirtualPath baseVirtualDir, bool allowCrossAppMapping)
{
if (this._wr == null)
{
throw new HttpException(SR.GetString("Cannot_map_path_withou t_context"));
}
if (virtualPath == null)
{
virtualPath = VirtualPath.Create(".");
}
VirtualPath path = virtualPath;
if (baseVirtualDir != null)
{
virtualPath = baseVirtualDir.Combine(virtualPath);
}
if (!allowCrossAppMapping)
{
virtualPath.FailIfNotWithinAppRoot();
}
string str = virtualPath.MapPathInternal();
if (((virtualPath.VirtualPathString == "/") && (path.VirtualPathString != "/"))
&& (!path.HasTrailingSlash && UrlPath.PathEndsWithExtraSlash(str)))
{
str = str.Substring(0, str.Length - 1);
}
InternalSecurityPermissions.PathDiscovery(str).Dem and();
return str;

}

public string MapPath(string path)
{
if (this._context == null)
{
throw new HttpException(SR.GetString("Server_not_available") );
}
return this._context.Request.MapPath(path);

}

public string MapPath()
{
return HostingEnvironment.MapPath(this);

}

Juan T. Llibre, asp.net MVP
asp.net faq :http://asp.net.do/faq/
foros de asp.net, en espanol :http://asp.net.do/foros/
======================================

"Michael Nemtsev" <nemt...@msn.comwrote in messagenews:3d*************************@msnews.mic rosoft.com...
Hello r...@rediffmail.com,
There are no difference in this aspect, because the Server.MapPath calls the _context.Request.MapPath(path)
inside its method
---
WBR, Michael Nemtsev [.NET/C# MVP] :: blog:http://spaces.live.com/laflour
"The greatest danger for most of us is not that our aim is too high and we miss it, but that it is too low and we
reach it" (c) Michelangelo
Server.MapPath returns the physical file path that corresponds to the
specified virtual path whereas 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 both
Response.Write(Server.MapPath("Hello.aspx"))
&
Response.Write(Request.MapPath("Hello.aspx"))
is C:\Inetpub\wwwroot\MyFolder\Hello.aspx. So what's the difference
between Server.MapPath & Request.MapPath?
Thanks- Hide quoted text -

- Show quoted text -
Thanks both of you for your inputs but Juan, being a ASP.NET newbie,
your response has left me in a tizzy! More so because I use VB.NET &
not C# & all the C# code you have cited has left me further confused!

BTW, I have come across the word "context" numerous times since I have
started learning ASP.NET but to be honest, I don't understand what
does it exactly mean. Can someone please explain me what does
"context" mean with respect to .NET? As such, I know what does
"context" mean in English!

If giving examples, please try using VB & not C#.

Ron

Oct 7 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: John Rebbeck | last post by:
I've got the directory f:\Company\Product set as web shared so it's got a virtual directory in the default web site on my test server's IIS. If I try to use Server.MapPath in that site it returns a...
3
by: Jake | last post by:
How can I pass the results of a form field submitted to this statement below? <% Set MyFileObj = Server.CreateObject("Scripting.FileSystemObject") '### This Works '###Set MyTextFile =...
5
by: KathyB | last post by:
If someone could just explain this to me...I just don't get it! I have an aspx page where I retrieve several session variables and use xmlDocument to transform xml file with xsl file into an...
3
by: Kian Goh | last post by:
Hi, Can somebody tell me why can't I use string strPath = Server.MapPath(Request.ApplicationPath); in protected void Application_Start(...) It compiles ok but an unhandled exception occurred...
0
by: Carl Gilbert | last post by:
Hi I am trying to get an online gallery to work (www.ngallery.org). I have managed to get it all working on my local host but I can not get it to work on my web space. The site can be found...
6
by: darrel | last post by:
I have some functions that are reading/writing to the file system. As such, they use server.mapPath a lot to navigate it all. Since I'm using these functions numerous times throughout my...
2
by: WB | last post by:
Hi, How can I access Server and Trace within a helper class? I have a webform that allows user to select a PDF file (on the web server) and render a jpeg image of that PDF file. The helper...
7
by: teo | last post by:
I need to use the 'Server.MapPath' function in the 'Session_End' event of the Global.asax file (to reach a folder and the clean some temporary files up), but it doesn't work: Sub...
2
by: =?Utf-8?B?dHBhcmtzNjk=?= | last post by:
Can anyone tell me the difference between server.mappath and request.mappath? Why would I use one vs. the other?
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.