Hi @Glenn ,
System.IO.DirectoryNotFoundException: Could not find a part of the path '/home/site/wwwroot/~assets/gallery
By default, in asp.net core application, the Static files are stored within the project's web root directory. The default directory is {content root}/wwwroot
, but it can be changed with the UseWebRoot method.
Please check your Startup.cs file, whether you have changed the default web root directory.
The default setting like this: use the app.UseStaticFiles();
:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
If you are using the default root directory, I suggest you could try to put the assets/gallery
folder in the wwwroot
, like this:
Then, in the OnGet method, you can get the file path via the IWebHostEnvironment
, code as below:
public class ImageIndexModel : PageModel
{
[BindProperty]
public string[] imgPaths { get; set; }
//required using Microsoft.AspNetCore.Hosting;
private IWebHostEnvironment _environment;
public ImageIndexModel (IWebHostEnvironment environment)
{
_environment = environment;
}
public void OnGet()
{
var filefolder = Path.Combine(_environment.WebRootPath, "assets", "gallery");
imgPaths = Directory.GetFiles(filefolder).Select(file => Path.GetFileName(file)).ToArray();
}
}
The result as below:
After that, you can republish the application to the Azure web application (after publishing, you can also use Kudu to check if the gallery folder contains image file) and check if the website works.
More detail information about the Static files, see Static files in ASP.NET Core.
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion