Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

asp.net

People also ask

What is server MapPath?

The MapPath method maps the specified relative or virtual path to the corresponding physical directory on the server.

What is server MapPath in MVC?

Many times we need to know the full path of remote server where we are hosting or the exact location of file but if we don't how we can't. Actually we have MapPath method which maps the specified relative or virtual path to the corresponding physical directory on the web server.


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


Server.MapPath() requires an HttpContext. HostingEnvironment.MapPath does not.