Net Core 2.2 IFileProvider DirectoryNotFoundException Error on Debug As Looks in C:\Program Files\IIS Express\wwwroot\FOLDER
I was able to debug in Net Core 2.1 but since updating the project to 2.2 I am now getting the following error on debug (Ctrl+F5 or just F5) and this happens in the latest version of Visual Studio 2017 as well as the latest 2019 beta. I just tried upgrading to the latest December .NET Core SDK but it made no difference. The error is:
An error occurred while starting the application.
DirectoryNotFoundException: C:\Program Files\IIS Express\wwwroot\FOLDER\
This is from code in the Startup folder which supplies product images:
services.AddSingleton<IFileProvider>(
new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/FOLDER")));
This folder exists in the wwwroot folder of the project complete with all images.
So the problem appears to be that IIS Express is not copying/referencing this folder correctly.
The same code caused no issues on NET Core 2.1 and I was able to successfully preview the web application prior to publish.
Even if I create the folder in question at C:\Program Files\IIS Express\ (wwwroot doesn't even exist here) then it still doesn't work so there is no way to preview the valid code without changing it.
Is this a bug or is there a way to work around this new issue?
Thanks
Robin
After further research I have now found a way to fix this.
Not sure if it is a bug or the functionality of Directory.GetCurrentDirectory() has changed but it seems the correct way to do this (which works in NET Core 2.2) is:
First amend the top part of Startup.cs as follows to make the environment variables accessible:
public Startup(IHostingEnvironment env)
{
_env = env;
}
private IHostingEnvironment _env;
Then amend the code in the ConfigureServices section as follows:
var webRoot = _env.WebRootPath;
services.AddSingleton<IFileProvider>(
new PhysicalFileProvider(
Path.Combine(webRoot, "FOLDER")));
As this references wwwroot already I had to amend the path I was using (above).
Now it works correctly and the same fix works on other projects.
thanks work
I can confirm I had the same issue after upgrading from 2.1 to 2.2. I think it's a bug.
And I can confirm it's known about and some sort of fix coming here - https://github.com/aspnet/Docs/issues/9865
Although this is from December 2018 and it's still a problem in Core 2.2.2.
Your fix above works but provides a slightly different path (webroot) to the original code - seems the correct variable to the matching path is _env.ContentRootPath
Most helpful comment
I can confirm I had the same issue after upgrading from 2.1 to 2.2. I think it's a bug.
And I can confirm it's known about and some sort of fix coming here - https://github.com/aspnet/Docs/issues/9865
Although this is from December 2018 and it's still a problem in Core 2.2.2.
Your fix above works but provides a slightly different path (webroot) to the original code - seems the correct variable to the matching path is
_env.ContentRootPath