Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core 3.1 WebRoot Path

I am pushing an external file into the wwwroot folder of my ASP.NET Core 3.1 application. I need to reference the path to provide a constructor variable during the services wire up.

In the Startup class I have added the IWebHostEnvironment to the constructor parameters:

public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
  Configuration = configuration;
  _env = env;
}

When debugging, it seems that _env.WebRootPath returns the path as it is in my source folder, rather than the path being executed, i.e.

It returns: <path to my source code>/wwwroot

Instead of the execution relative path: <path to my source code>/bin/Debug/netcoreapp3.0/wwwroot

How do i get it to return the correct, execution relative path?

like image 943
BlackSpy Avatar asked Oct 13 '25 09:10

BlackSpy


2 Answers

Maybe try something like the following:

var rootDir  = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;

For further information/help take a glance on this article: Getting the Root Directory Path for .Net Core Applications

like image 53
vasilisdmr Avatar answered Oct 15 '25 00:10

vasilisdmr


It's because by default the content-root path is the directory where the project is located rather than its output.

If you want to change that behavior, you have to call this line when building the web-host.

.UseContentRoot(AppContext.BaseDirectory);

Then the path will change to <ProjectPath>\Debug\netcoreapp3.1 or whatever you are compiling to.

like image 45
alsami Avatar answered Oct 14 '25 23:10

alsami