Server.MapPath Equivalent in ASP.NET Core

4.38 (8 votes)

Web Developers who use Microsoft technologies have always relied on the Server.MapPath method to resolve file paths in classic ASP and ASP.NET Web Forms and MVC versions up to and including 5. This method has not been included in ASP.NET Core, so what do you use instead?

One of the services that's included by default when you create an ASP.NET Core application is IHostingEnvironment. The service has two properties of particular interest: ContentRootPath and WebRootPath.

ContentRootPath resolves to the application's base path. This is the location of the web.config, project.json, source code and other files not intended to be served to browsers. The WebRootPath property gets the physical file path to the directory that houses files intended to be browsable. By default, this is the wwwroot folder in the application.

Great! So how do you access the IHostingEnvironment properties? It's easy, really. Since IHostingEnvironment is added to the application's services by the framework, you can simply inject the service into the constructor of your controller and the built-in dependency injection system will resolve it for you:

You will need to add a using directive for Microsoft.AspNetCore.Hosting to make IHostingEnvironment available to your controller class, and you will have to create a constructor for the controller.

Changing WebRootPath

As I mentioned, the default location of WebRootPath is the wwwroot folder, but what if for some reason you wanted to change that? You can use the UseWebRoot extension method on the WebHostBuilder class to set your preferred location. You can do this in the Main method of the Program class:

You might also like...

Date Posted:
Last Updated:
Posted by:
Total Views to date: 10854

2 Comments

- Carl T.

Very succinct and easy to follow. Worked perfectly the first time for me. Thanks!!

- Aghil

Hi,

Thanks, it was really good. However, how can we access the in the Classes?

Is there any way not to pass IHostingEnvironment object to the class?

Recent Comments

Obinna Okafor 26/05/2017 16:16
In response to Razor Pages - The Elevator Pitch
Thank you for this wonderful piece, Mike. I need more of these...Keep them coming - anything more...

Cyrus 26/05/2017 06:00
In response to Razor Pages - The Elevator Pitch
There are some concern about razor pages performance. Is it faster or slower than MVC? would you a a...

Cyrus 26/05/2017 05:44
In response to Razor Pages - Understanding Handler Methods
well done, thank you....

Satyabrata Mohapatra 23/05/2017 11:41
In response to Razor Pages - Understanding Handler Methods
Nice and easy !! Great post....

Cyrus 16/05/2017 19:55
In response to Razor Pages - Getting Started With The Preview
There is something wrong related to microsoft.dotnetcore.mvc.taghelpers! if you remove it from page...

Cyrus 16/05/2017 10:18
In response to Razor Pages - Getting Started With The Preview
well done mike, it was very useful, I really appreciate that....

Satyabrata Mohapatra 16/05/2017 07:21
In response to Razor Pages - Getting Started With The Preview
Finally!!!! web pages in asp.net core!!! Super excited !!!! Thank u sir for sharing.....Awaiting on...

Daniele 14/03/2017 10:24
In response to Working With Zip Files In ASP.NET MVC
is it possible give to the user a progress bar of the zipping process? Thanks in advance. ...

Suraj 13/03/2017 22:20
In response to Working With Zip Files In ASP.NET MVC
Very nice article. Thanks....

Satyabrata Mohapatra 19/02/2017 03:01
In response to Free SSL Certificates On IIS With LetsEncrypt
Thanks for sharing. Learned a lot !!...