1

I'm currently working on a .NET Standard 2.1 Blazor WebAssembly application. I try to read file paths from my wwwroot directory.

The Blazor WebAssembly App is NOT Asp.NET Core hosted.

enter image description here

I try to read all file paths with the ending .js from my Main method in the Program.cs like this:

var filePaths = ReadWwwRootJSFilePaths();
private static string[] ReadWwwRootJSFilePaths()
        {
            Console.WriteLine("CurrentDirectory: " + Directory.GetCurrentDirectory());
            var dir = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "js");
            Console.WriteLine("dir: " + dir);
            var files = Directory.GetFiles(dir);
            foreach (string file in files)
            {
                Console.WriteLine("file: " + file);
            }
            return files;
        }

When I run this I only get an error message in my browser: System.IO.DirectoryNotFoundException

enter image description here

Do you know how to solve this problem? Do you know how to read file paths from the wwwroot dir in a Blazor WASm (not ASP.NET Core hosted) application?

Perhaps yet I don't quite get the concept of Blazor WebAssembly (not ASP.NET Core hosted). Is it actually possible to use C# .NET Core functionalities?

Vy Do
  • 46,709
  • 59
  • 215
  • 313
azzurro123
  • 521
  • 2
  • 10
  • 19
  • 1
    Does this answer your question? [Reading server-side files using Blazor](https://stackoverflow.com/questions/55407886/reading-server-side-files-using-blazor) – Diado Jul 24 '20 at 13:02
  • 2
    Browsers cannot read from the computer's drive. You might be able to achieve your goal if you use Blazor + Electron to build your app, or perhaps Blazor Mobile Bindings might be a better choice? – Peter Morris Jul 24 '20 at 13:25

1 Answers1

7

Yes, you can simply perform HTTP calls to the files in your wwwroot.

To see this done in action, create a WebAssembly stand alone project, in which the FetchData calls a json file located in the wwwroot. Here's some code:

protected override async Task OnInitializedAsync()
    {
        forecasts = await Http.GetJsonAsync<WeatherForecast[]>("sample-data/weather.json");
    }
enet
  • 41,195
  • 5
  • 76
  • 113