Differencebetween Server.Mappath and Hostingenvironment.Mappath

What is the difference between Server.MapPath and HostingEnvironment.MapPath?

Server.MapPath() eventually calls HostingEnvironment.MapPath(), but it creates a VirtualPath object with specific options:

The VirtualPath object passed to HostingEnvironment.MapPath() is constructed like this:

VirtualPath.Create(path, VirtualPathOptions.AllowAllPath|VirtualPathOptions.AllowNull);

Edit: in reality, the only difference is that you are allowed to pass null to Server.MapPath(), but not to HostingEnvironment.MapPath()

Difference between Server.MapPath and Page.MapPath

I have seem through Reflector and it seems both methods end up calling the same base method.

So you can use whichever fits better.

If you don't have the Page, you can get it through the context handler. (It will be null if you are not actually in a page).

var Page = HttpContext.Current.Handler as Page;

Server.MapPath(.), Server.MapPath(~), Server.MapPath(@\), Server.MapPath(/). What is the difference?

Server.MapPath specifies the relative or virtual path to map to a physical directory.

  • Server.MapPath(".")1 returns the current physical directory of the file (e.g. aspx) being executed
  • Server.MapPath("..") returns the parent directory
  • Server.MapPath("~") returns the physical path to the root of the application
  • Server.MapPath("/") returns the physical path to the root of the domain name (is not necessarily the same as the root of the application)

An example:

Let's say you pointed a web site application (http://www.example.com/) to

C:\Inetpub\wwwroot

and installed your shop application (sub web as virtual directory in IIS, marked as application) in

D:\WebApps\shop

For example, if you call Server.MapPath() in following request:

http://www.example.com/shop/products/GetProduct.aspx?id=2342

then:

  • Server.MapPath(".")1 returns D:\WebApps\shop\products
  • Server.MapPath("..") returns D:\WebApps\shop
  • Server.MapPath("~") returns D:\WebApps\shop
  • Server.MapPath("/") returns C:\Inetpub\wwwroot
  • Server.MapPath("/shop") returns D:\WebApps\shop

If Path starts with either a forward slash (/) or backward slash (\), the MapPath() returns a path as if Path was a full, virtual path.

If Path doesn't start with a slash, the MapPath() returns a path relative to the directory of the request being processed.

Note: in C#, @ is the verbatim literal string operator meaning that the string should be used "as is" and not be processed for escape sequences.

Footnotes

  1. Server.MapPath(null) and Server.MapPath("") will produce this effect too.

Microsoft Web API: How do you do a Server.MapPath?

You can use HostingEnvironment.MapPath in any context where System.Web objects like HttpContext.Current are not available (e.g also from a static method).

var mappedPath = System.Web.Hosting.HostingEnvironment.MapPath("~/SomePath");

See also What is the difference between Server.MapPath and HostingEnvironment.MapPath?

Server.MapPath() can't go up 2 levels?

In windows there is no .../ path.

  • ./ means the current directory
  • ../ means one level above

Note: You can think the quantity of . are the quantity of levels, but that isn't so.

2 Levels above are ../../, 3 Levels ../../../ and so on.

Server.MapPath VS. @Href(~/SomeFile.cshtml)

More secure? I don't see a reason why - but I would use neither in Razor. Here's why:

As far as I remember, the Href function is from the ASP.NET 1.0 times. When doing WebForms (!) code you can just paste the ~-URL <a href="~/" runat="server">Some link</a>.

However, if you're doing ASP.NET MVC (which I assume you're doing since you're using Razor) you should rather use Url.Content() which fits with Url.Action() name-wise.



Related Topics



Leave a reply



Submit