Search code examples
blazorwebassemblystatic-filesfile-not-foundwwwroot

Blazor WASM File does not exist (but is readable?)


I have a Blazor WASM PWA that I want to be standalone (if possible).

Currently, I am trying to manipulate a json file under wwwroot

Location of Json File

Now, while I am able to read its contents via HttpClient.GetFromJsonAsync, all other attempts to access the File fail.

What I've tried:

  • Check if the File exists via File.Exists -> returns false
  • Post objects via HttpClient.PostAsJsonAsync -> returns 404 for the Json-File

I know that in WebAssembly, Files are cached/loaded into the browser, so I get why I should be unable to locate the File with a Path like "/datasource-json/Ingredient.json", but why does "GetFromJsonAsync" work with that path then?

Is there something crucial I am missing, and how can I work my way around this? For this particular project, I do not want to use another Api-Project + Entity Framework if possible.


Solution

  • Wasm runs entirely on the browser and so you have literally no file system. If you had any it would be the client machine, but that would be a major security breach.

    Why getting the file from http works is because the server provides GET access to all files in the web root.

    Why POST does not work is because posting something requires a server endpoint that listens for that data on that URL and can understand the format you send. So, you need some sort of controller that will have a specific URL that will not be the same as the file URL