Get Root Directory of the Project in ASP.NET Core

Today in this article we will discuss the generic approach to get root directory in ASP.NET Core for any .NET or .NET Core application programmatically.

The approach mentioned below works fine for Windows or Mac-OS machines as well.

There are many approaches to this simple requirement but not often do these approaches work due to many reasons like the type of project Ex. Test Project, WebAPI, MVC, etc, and their directory structure required for project build, etc.

Today in this article, we will cover below aspects,

Understanding IWebHostEnvironment

IWebHostEnvironment interface provides information about the web hosting environment an application is running.

IWebHostEnvironment implements IHostEnvironment.

It provides access to the absolute path to the directory that contains the web-servable application content files.

Accessing root directory in Startup.cs

Please inject IWebHostEnvironment in the Startup constructor as below,

public Startup(IConfiguration configuration, IWebHostEnvironment env)
        {
            contentRootPath = env.ContentRootPath;
            webRootPath = env.WebRootPath;
            projectRootPath = AppContext.BaseDirectory;
        }

Understanding ContentRootPath vs WebRootPath

Let’s understand the ContentRootPath and WebRootPath in brief.

What is ContentRootPath

This property provides Gets or sets the absolute path to the directory that contains the application content files.

In the bellow example we get the project directory location i.e C:\Users\all_i\source\repos\mvcpoc

Root Directory in ASP.NET Core using WebRootPath

This property provides Gets or Sets the absolute path that contains the web-servable application content files.

  • This property remains empty in plain ASP.NET Core API project.

  • If you are creating an ASP.NET Core MVC project template that contains web-servable application content files like HTML, javascript, etc then this property gives the absolute path.

  • Provides of the absolute path of wwwroot

Example "C:\Users\source\repos\mvcpoc\wwwroot"

blank

Note: Please note that MVC templates are similar to API template except it provides web-servable content.

Additionally you can observe such project templates using static content like html and other resources app.UseStaticFiles() in the API pipeline.

AppContext.BaseDirectory

Gets the pathname of the base directory that the assembly resolver uses to probe for assemblies. This is mostly your final .exe/library binary location.

projectRootPath = AppContext.BaseDirectory;

blank

Please see the below example,

Get Root Directory of the Project in ASPNET Core

Example

C:\Users\all\source\repos\mvcpoc\bin\Debug\netcoreapp3.1\

AppContext.BaseDirectory can also be used in the TEST project. It works perfectly fine and gives your base directory path.

Alternatively one can also use the below way using GetExecutingAssembly which I found to be equivalent to AppContext.BaseDirectory and produces the same result.

string projectRootPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

Accessing Root directory in Controller or other modules

You can follow the same technique to DI IWebHostEnvironment from Constructor injection from your Controller or any other class.

blank

As you can see you can inject IWebHostEnvironment interface in any place where you need access to the root path or base root directory.

Other alternatives are using typesafe custom configuration class objects which can be DI as required.

References :

That’s all! Happy Coding



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



2 thoughts on “Get Root Directory in ASP.NET Core – Guidelines

    1. Hi Hassan, thanks for your query. okay… I believe the above approach should help you implement your requirements…do you see any issue or have any specific questions?

Leave a Reply

Your email address will not be published. Required fields are marked *