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 toHostingEnvironment.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 executedServer.MapPath("..")
returns the parent directoryServer.MapPath("~")
returns the physical path to the root of the applicationServer.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 returnsD:\WebApps\shop\products
Server.MapPath("..")
returnsD:\WebApps\shop
Server.MapPath("~")
returnsD:\WebApps\shop
Server.MapPath("/")
returnsC:\Inetpub\wwwroot
Server.MapPath("/shop")
returnsD:\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
Server.MapPath(null)
andServer.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
Inheritance with Base Class Constructor with Parameters
Accessing UI Control from Backgroundworker Thread
Better Way to Query a Page of Data and Get Total Count in Entity Framework 4.1
Copy Rows from One Datatable to Another Datatable
Calling a Soap Service in .Net Core
Command to Close an Application of Console
How to Refresh Datasource of a Listbox
Textrenderer.Measuretext and Graphics.Measurestring Mismatch in Size
Change Border Color in Textbox C#
Inconsistency in Divide-By-Zero Behavior Between Different Value Types
Binding Listbox to List<Object> in Winforms
Httpclient Not Supporting Postasjsonasync Method C#
How to Use Showdialog Without Blocking All Forms
How to Convert Ienumerable to Observablecollection
What Is the Fastest Way to Combine Two Xml Files into One
Wpf C# Path: How to Get from a String with Path Data to Geometry in Code (Not in Xaml)
How to Get Little Endian Data from Big Endian in C# Using Bitconverter.Toint32 Method