Server.MapPath & Request.MapPath

Discussion in 'ASP .Net' started by rn5a@rediffmail.com, Oct 6, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. Hello ,

    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
    >
     
    Michael Nemtsev, Oct 6, 2007
    #2
    1. Advertising

  3. 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(virtualPath));
    }

    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(virtualPath), 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).Demand();
    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" <> wrote in message news:...
    > Hello ,
    >
    > 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
    >>

    >
    >
     
    Juan T. Llibre, Oct 6, 2007
    #3
  4. Guest

    On Oct 6, 6:45 am, "Juan T. Llibre" <> wrote:
    > 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(virtualPath));
    >
    > }
    >
    > 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(virtualPath), 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).Demand();
    > 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" <> wrote in messagenews:...
    > > Hello ,

    >
    > > 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, 2007
    #4
  5. re:
    !> 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!

    The reason I cited the C# code is that ASP.NET ( yes, all of it ) is written in C# !

    I used Lutz Roeder's Reflector.Net to look at the code within system.web,
    and -after searching for MapPath with Reflector- found those instances.

    Pick up a copy...it's free :

    http://www.aisto.com/roeder/dotnet/

    ....and use it to peek inside any ASP.NET assembly.

    You'll increase your learning speed very quickly if you understand what runs
    inside the ASP.NET methods/assemblies/properties you use in your code.

    re:
    !> 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?

    Context, as used in ASP.NET, means the link to the request (httpcontext) made by the client,
    which ASP.NET uses as reference in order to send back an appropiate reply.

    Usually, context is tied to "current", i.e. "current context",
    which simply means the httpcontext associated with the current request.

    Basically, an HttpContext object contains information associated with the current page.

    If you're using inline code, you can just reference "Context".

    If you're using code-behind, you must reference httpContext.Current.

    The httpContext object provides access to the intrinsic
    Request, Response, and Server properties for the request.

    See Scott Allen's article at : http://www.odetocode.com/Articles/112.aspx




    Juan T. Llibre, asp.net MVP
    asp.net faq : http://asp.net.do/faq/
    foros de asp.net, en espaƱol : http://asp.net.do/foros/
    ======================================
    <> wrote in message news:...
    > On Oct 6, 6:45 am, "Juan T. Llibre" <> wrote:
    >> 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(virtualPath));
    >>
    >> }
    >>
    >> 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(virtualPath), 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).Demand();
    >> 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" <> wrote in messagenews:...
    >> > Hello ,

    >>
    >> > 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
    >
     
    Juan T. Llibre, Oct 7, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon
    Replies:
    5
    Views:
    11,304
    Scott M.
    Jan 17, 2010
  2. Peter Row
    Replies:
    0
    Views:
    481
    Peter Row
    Mar 1, 2004
  3. =?Utf-8?B?dHBhcmtzNjk=?=

    server.mappath vs. request.mappath

    =?Utf-8?B?dHBhcmtzNjk=?=, Mar 10, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    1,197
    Juan T. Llibre
    Mar 11, 2007
  4. AAaron123
    Replies:
    11
    Views:
    1,213
    AAaron123
    Sep 9, 2009
  5. Dan Sikorsky
    Replies:
    6
    Views:
    297
    Jeff Cochran
    Nov 20, 2003
Loading...

Share This Page