Server.MapPath Equivalent in ASP.NET Core

3.79 (14 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:

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

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

Thomas 05/03/2018 00:59
In response to I'm Not Writing A Book On Razor Pages
There's a typo on this page: = true)] should be [BindProperty(SupportsGet = true)]...

Rolf Herbert 04/03/2018 19:25
In response to I'm Not Writing A Book On Razor Pages
So is MS deprecating razor Web Pages..? Is it dead..? I wish they would stop killing things so its...

Borut 17/02/2018 12:59
In response to I'm Not Writing A Book On Razor Pages
Mike, is it possible that Web Pages and Razor Pages "live" together in one web application? I a I...

hrboyce 09/02/2018 04:44
In response to I'm Not Writing A Book On Razor Pages
Mike, First thanks for doing this but I have to ask, any chance you would consider converting one of...

aziz sallam 07/02/2018 10:18
In response to I'm Not Writing A Book On Razor Pages
u are a great man...

Satyabrata Mohapatra 31/01/2018 11:36
In response to I'm Not Writing A Book On Razor Pages
This is a great news!!!! Thanks...

tangdf 30/01/2018 07:25
In response to I'm Not Writing A Book On Razor Pages
=> { o.ConfigureFilter(new IgnoreAntiforgeryTokenAttribute()); }); The extension method does...

Obinna Okafor 30/01/2018 04:02
In response to I'm Not Writing A Book On Razor Pages
Thank you very much. I would like to see a project built from scratch using Razor Pages. And it show...

rachida Dukes 31/10/2017 13:52
In response to Customising Identity in Razor Pages
Thanks again for this wonderful tutorial. I followed all the steps in this section called: Adding...

Rachida 31/10/2017 12:06
In response to Customising Identity in Razor Pages
Thanks very much for this wonderful tutorial, it helped a lot....