Server.MapPath Equivalent in ASP.NET Core

3.69 (13 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: 22837


- Carl T.

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

- Aghil


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

Pam 30/08/2017 11:30
In response to Sending Email in Razor Pages
Mike, RazorPages sound like a nice choice for somebody still working in ASP classic who wants to to...

Robby Robson 15/08/2017 00:43
In response to Routing in Razor Pages
Mike: great stuff. Now that .Core Standard 2.0 is formally out, how soon will you rewrite your book...

Satyabrata Mohapatra 28/07/2017 08:59
In response to Sending Email in Razor Pages
Bit off topic, but congratulation sir for your MVP award. You deserve it !!!...

Satyabrata Mohapatra 23/07/2017 16:43
In response to Razor Pages - The Elevator Pitch
@Dale Severin You can continue to build apps using web pages....

Satyabrata Mohapatra 23/07/2017 16:40
In response to Sending Email in Razor Pages
Thanks for sharing...learned a lot...

Gfw 22/07/2017 11:53
In response to Sending Email in Razor Pages
Question... Does System.Net.Mail support SSL?...

Dale Severin 20/07/2017 03:38
In response to Razor Pages - The Elevator Pitch
I work with razor web pages extensively. I appreciate the rapid development it permits me to I am as...

Obinna Okafor 14/07/2017 01:19
In response to Routing in Razor Pages
Thank you, Mike. Good post....

Satyabrata Mohapatra 11/07/2017 16:02
In response to Routing in Razor Pages
Very powerful routing system!!...

Cyrus 05/07/2017 03:41
In response to Razor Pages - Getting Started With The Preview
How can I trim packages and services as much as possible to use just razor pages? I don’t want to to...