Getting the Web Root Path and the Content Root Path in ASP.NET Core

If you wanted to map relative or virtual paths to physical directories on the server in classic ASP.NET applications, you were always able to use the Server.MapPath() method to find the physical path of the web root directory like this:

// Classic ASP.NET

public class HomeController : Controller
{
    public ActionResult Index()
    {
        string physicalWebRootPath = Server.MapPath("~/");
        
        return Content(physicalWebRootPath);
    }
}

In ASP.NET Core, however, the Server.MapPath() method doesn't exist anymore. In fact, the Controller base class doesn't even have a Server property.

ASP.NET Core

Starting with ASP.NET Core RC2, the physical paths to both the web root and the content root directories can be retrieved by injecting and querying the IHostingEnvironment service:

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;

namespace AspNetCorePathMapping
{
    public class HomeController : Controller
    {
        private readonly IHostingEnvironment _hostingEnvironment;

        public HomeController(IHostingEnvironment hostingEnvironment)
        {
            _hostingEnvironment = hostingEnvironment;
        }

        public ActionResult Index()
        {
            string webRootPath = _hostingEnvironment.WebRootPath;
            string contentRootPath = _hostingEnvironment.ContentRootPath;

            return Content(webRootPath + "\n" + contentRootPath);
        }
    }
}

I have created a demo .NET Core application on my machine in the ~/Code/AspNetCorePathMapping directory. If I run it, the above controller action returns the following two paths:

Notice the distinction between web root and content root: The web root is the root directory from which static content is served, while the content root is the application base path. You can then use either path in conjunction with Path.Combine() to construct a physical file path to a specific file or directory.

ASP.NET Core RC1

Before ASP.NET Core RC2 — that is, up until ASP.NET Core RC1 — the application base path was available via IApplicationEnvironment.ApplicationBasePath instead of IHostingEnvironment.ContentRootPath:

using Microsoft.AspNet.Mvc;
using Microsoft.Extensions.PlatformAbstractions;

namespace AspNetCorePathMapping
{
    public class HomeController : Controller
    {
        private readonly IApplicationEnvironment _appEnvironment;

        public HomeController(IApplicationEnvironment appEnvironment)
        {
            _appEnvironment = appEnvironment;
        }

        public ActionResult Index()
        {
            return Content(_appEnvironment.ApplicationBasePath);
        }
    }
}

Use the coupon code LAUNCHDAY for $10 off!

Learn React

9 Comments

Mario Vernari

Many thanks, Marius! Your post seems the most clear I found after hitting my head against the wall. Just a little correction to your RC2-version: the namespace to import is "Microsoft.AspNetCore.Hosting". My goal is to serve pictures stored off the ContentRootPath, and I had hard time trying to find a solution (no docs at all). It could help to other people knowing that you can't use the classic "File" method exposed from the underlying Controller, otherwise an exception will be raised for a not found file. You must use the "PhysicalFile" instead. This could help: https://github.com/aspnet/Mvc/issues/4103

Vladislav Sarunov

Hey Marius, I was hitting the wall with the Server.MapPath, but now you cleared it out big thanks, big up :)

Alex

Thanks, this is really helpful, I'm following the course in Pluralsight (Building a Web App with ASP.NET Core RC1, MVC 6, EF7 & AngularJS) and I was hitting my head against the wall for a while

Ivaylo

It works great on a windows machine but there is a problem when compiling the very same code on a mac. Trying to run this code I get an error that the path is invalid:

    public static IApplicationBuilder ServeStaticFiles(this IApplicationBuilder app, 
        IHostingEnvironment env)
    {
        string root = Path.Combine(env.ContentRootPath, "node_modules");
        var physicalFileProvider = new PhysicalFileProvider(root);  // The directory name /Users/..[full path]..../node_modules/ is invalid.
        var options = new StaticFileOptions();
        options.RequestPath = "/node_modules";
        options.FileProvider = physicalFileProvider;
        app.UseStaticFiles(options);
        return app;
    }
TemRhythm

Thank you! Very helpful!

rudi

it is possible to get list of files?