Hosted Blazor WASM WebRootPath is null

279 views Asked by At

I'm usually not a front end guy, so maybe this is a stupid question, but I'm stumped.

I'm working on dealing with file uploads in a hosted Blazor WASM app. WebRootPath is null in the controller, which sort of makes sense to me since the wwwroot folder is on the client.

Ok, since I know that when I publish the wwwroot folder will be available one level up from the ContentRootPath, I can get to it in production. But, how do I get to it in development? And what is the best way to make uploads work in development and production?

Basically, how do I access the client project's wwwroot folder in both enviornments?

1

There are 1 answers

0
Сергей Гущин On

There`s a very hacky way of getting the wwwroot path in development

//_appEnvironment is your injected IWebHostEnvironment
var compositeProvider = ((CompositeFileProvider)_appEnvironment.WebRootFileProvider).FileProviders.First();
var providers = (IFileProvider[])compositeProvider.GetType().GetProperty("FileProviders", BindingFlags.Instance | BindingFlags.NonPublic)!.GetValue(compositeProvider)!;
var paths = providers.OfType<PhysicalFileProvider>().Select(x => x.Root).OrderBy(x => x.Length);
string wwwroot = paths.FirstOrDefault(x => Path.GetFileName(Path.GetDirectoryName(x)) == "wwwroot");

I must also warn you that i did not properly tested this method on multiple platforms, so it may not work for you.