1

The question is related to Blazor wasm hosted App.

I am resolving _webHostingEnvironment in constructor of the controller.

_webHostingEnvironment = serviceProvider.GetRequiredService<IWebHostEnvironment>();

I am running into issue where I don't get correct value for the virtual director's physical path.

My app generates a QR code which I need to store in the images folder of virtual directory.

var webRootPath = "";
if (string.IsNullOrWhiteSpace(_webHostingEnvironment.WebRootPath))
     webRootPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot");
else
   webRootPath = _webHostingEnvironment.WebRootPath

Firstly _webHostingEnvironment.WebRootPath is always null, hence the code always gets the path from Directory.GetCurrentDirectory().

Directory.GetCurrentDirectory provides the path of Server, but not the actual deployed path of wwwroot which is further down like /bin/debug/net6.0/wwwroot.

Here is the screenshot of the code in question

enter image description here

Now I could append that static path to get full path, but no sure what to use when I deploy it on Azure App Service or while using other deployment models

Mark Cooper
  • 6,738
  • 5
  • 54
  • 92
NSS
  • 1,835
  • 2
  • 29
  • 66
  • Blazor WASM runs on the browser. There's no virtual path, much less a physical path. Do you have a *hosted* project? In that case, is the question how to get the `WebRoot` in one of your controllers? Where does `_webHostingEnvironment` come from? That's not something that can be cached. If `WebRootPath` is empty, don't assume .NET Core is broken. Something is wrong with your application and `Directory.GetCurrentDirectory()` will almost certainly return the wrong path. – Panagiotis Kanavos Sep 27 '21 at 12:24
  • I did mention it is Blazor wasm hosted app The code in question is running inside Server controller. I do get ContentRootPath, but it is same as GetCurrentDirectory, which is way off from the Virtual Path – NSS Sep 27 '21 at 12:55
  • Which means the question has nothing to do with Blazor. Post your ASP.NET Core code. Where did `_webHostingEnvironment`? Which .NET 6 version are you using? Does this code run in a controller? `Startup`? Are you using minimal APIs ? – Panagiotis Kanavos Sep 27 '21 at 12:56
  • I am resolving that in constructor of the controller. _webHostingEnvironment = serviceProvider.GetRequiredService(); – NSS Sep 27 '21 at 12:59
  • Post your code in the question itself, *and* your service registration code. You shouldn't have to use *any* of the code you posted, which means something weird is going on. As [this asnwer explains](https://stackoverflow.com/a/59305508/134204) you won't get a `WebRootPath` if there's no `wwwroot` in the template, although you'll get a `ContentRootPath`. You shouldn't use *either* though. Modifying the contents of your web site's root is a security risk. In ASP.NET Core you can specify multiple file providers and serve/store files from them – Panagiotis Kanavos Sep 27 '21 at 13:14
  • Or you can specify a storage folder in configuration settings, and inject it into controllers, just like any other setting. – Panagiotis Kanavos Sep 27 '21 at 13:16
  • Post the *code*, not a screenshot of the code. Images can't be copied, compiled or googled – Panagiotis Kanavos Sep 28 '21 at 08:20

0 Answers0