Tuesday, March 13, 2012

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?

ThanksHello rn5a@.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

Quote:

Originally Posted by

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
>


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_without_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" <nemtsev@.msn.comwrote in message news:3d9fba1aec828c9d64fc67e45f0@.msnews.microsoft. com...

Quote:

Originally Posted by

Hello rn5a@.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
>

Quote:

Originally Posted by

>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
>>


>
>


On Oct 6, 6:45 am, "Juan T. Llibre" <nomailrepl...@.nowhere.comwrote:

Quote:

Originally Posted by

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_without_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:3d9fba1aec828c9d64fc67e45f0@.msnews.mic rosoft.com...

Quote:

Originally Posted by

Hello r...@.rediffmail.com,


>

Quote:

Originally Posted by

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


>

Quote:

Originally Posted by

--
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


>

Quote:

Originally Posted by

Quote:

Originally Posted by

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


>

Quote:

Originally Posted by

Quote:

Originally Posted by

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


>

Quote:

Originally Posted by

Quote:

Originally Posted by

&


>

Quote:

Originally Posted by

Quote:

Originally Posted by

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


>

Quote:

Originally Posted by

Quote:

Originally Posted by

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


>

Quote:

Originally Posted by

Quote:

Originally Posted by

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

0 comments:

Post a Comment