ASP.NET Core: Getting Project Root Directory Path

In this article, we will show you approaches how to get the root directory/folder of an ASP.NET Core web project.

In fact, we often do this when we want to read or write our resource files which are stored within the project. For example, read an XML configuration file, read image/sound file, or write data to log file, etc.

Here are approaches on how to get root directory of an ASP.NET Core web project, let’s see how it works and use that fits your needs:

  1. Getting root directory within Startup.cs
  2. Getting root directory within a Controller
  3. Getting root directory using System.AppContext
  4. Getting root directory using System.AppDomain
  5. Getting root directory using System.IO.Path.GetDirectoryName
  6. Getting root directory using ApplicationEnvironment.ApplicationBasePath

1. Getting root directory within Startup.cs

  • Using IConfiguration in Startup’s constructor method:
Startup.cs
public Startup(IConfiguration configuration)
{
    string rootDirectory = configuration.GetValue<string>(WebHostDefaults.ContentRootKey);
}
  • Inject IHostingEnvironment in Startup’s constructor method:
Startup.cs
public Startup(IConfiguration configuration, IHostingEnvironment env)
{
    string rootDirectory = env.ContentRootPath;
}

 

2. Getting root directory within a Controller

Inject IHostingEnvironment in your Controller’s constructor method:

HomeController.cs
public HomeController(IHostingEnvironment env)
{
    string rootDirectory = env.ContentRootPath;
}

 

3. Getting root directory using System.AppContext

AspNetCoreHelper.cs
using System;

namespace MyAspNetCoreWebApp.Helpers
{
    public class AspNetCoreHelper
    {
        public static string ContentRootPath
        {
            get
            {
                string rootDirectory = AppContext.BaseDirectory;
                if (rootDirectory.Contains("bin"))
                {
                    rootDirectory = rootDirectory.Substring(0, rootDirectory.IndexOf("bin"));
                }
                return rootDirectory;
            }
        }
    }
}

In this approach, if you run the app from Visual Studio, you will a directory path like this “%projectfolder%\bin\debug\netcoreapp2.0” from the AppContext.BaseDirectory, and it is not a root directory. To get root directory from it, we need to remove “bin” and later by using Substring as shown in the code.
 

4. Getting root directory using System.AppDomain

AspNetCoreHelper.cs
using System;

namespace MyAspNetCoreWebApp.Helpers
{
    public class AspNetCoreHelper
    {
        public static string ContentRootPath
        {
            get
            {
                string rootDirectory = AppDomain.CurrentDomain.BaseDirectory;
                if (rootDirectory.Contains("bin"))
                {
                    rootDirectory = rootDirectory.Substring(0, rootDirectory.IndexOf("bin"));
                }
                return rootDirectory;
            }
        }
    }
}

Like the above System.AppContext approach, we also need to remove “bin” and later from the path returned from the AppDomain.CurrentDomain.BaseDirectory property using Substring as shown in the code.
 

5. Getting root directory using System.IO.Path.GetDirectoryName

AspNetCoreHelper.cs
using System.IO;
using System.Reflection;

namespace MyAspNetCoreWebApp.Helpers
{
    public class AspNetCoreHelper
    {
        public static string ContentRootPath
        {
            get
            {
                string rootDirectory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
                if (rootDirectory.Contains("bin"))
                {
                    rootDirectory = rootDirectory.Substring(0, rootDirectory.IndexOf("bin"));
                }
                return rootDirectory;
            }
        }
    }
}

In this approach, we also need to remove “bin” and later from the Path.GetDirectoryName result like right above approach.
 

6. Getting root directory using ApplicationEnvironment.ApplicationBasePath

AspNetCoreHelper.cs
using Microsoft.DotNet.PlatformAbstractions;

namespace MyAspNetCoreWebApp.Helpers
{
    public class AspNetCoreHelper
    {
        public static string ContentRootPath
        {
            get
            {
                string rootDirectory = ApplicationEnvironment.ApplicationBasePath;
                if (rootDirectory.Contains("bin"))
                {
                    rootDirectory = rootDirectory.Substring(0, rootDirectory.IndexOf("bin"));
                }
                return rootDirectory;
            }
        }
    }
}

Please consider when you decide to use this approach because according to what is written in this page, this approach seems to be removed in the future versions of .NET Core:

In ASP.NET Core 2.0 we are removing the Microsoft.Extensions.PlatformAbstractions package as it is no longer necessary to maintain this abstraction.

Leave a Reply

avatar