Hi @volk volk, please allow me to add more information relate to my test result.
First, I agree with @AgaveJoe to change static file configuration to realize your requirement. I have a method to read file content in my controller, and I also have images folder inside the wwwroot folder.
public IActionResult Index()
{
var rootRoute = Environment.CurrentDirectory;
try {
//var route = rootRoute + "\\Cert\\myfile.txt";
var route = "C:\\files\\test.txt";
var content = System.IO.File.ReadAllText(route);
ViewData["fileContent"] = content;
}catch(Exception ex) {
ViewData["fileContent"] = "exception when read the file";
}
ViewData["CurrentDirectory"] = rootRoute;
return View();
}
<img src="~/images/Myimage.png" class="img" alt="my image" />
<img src="~/StaticFiles/images/red-rose.jpg" class="img" alt="A red rose" />
<div>@ViewData["CurrentDirectory"]</div>
<div>@ViewData["fileContent"]</div>
Like you see, when I didn't set the app.UseStaticFiles(new StaticFileOptions
configuration in program.cs, I can still access the file content out of my project, but since it's didn't include into the application, so I can't access the images directly via url localhost:port/myCustomFileFolder/myImg.png
Upon adding the configuration in Program.cs, the image loaded.
The test above occurred in local VS 2022, and after publishing to IIS(I created a new IIS website which didn't change any other configuration), it worked as well. I trust if you got access denied issue, you might try to change the application pool identity to local system
for a test.