I have an image in wwwroot/img folder and want to use it in my server side code.
How can I get the path to this image in code?
The code is like this:
Graphics graphics = Graphics.FromImage(path)
I have an image in wwwroot/img folder and want to use it in my server side code.
How can I get the path to this image in code?
The code is like this:
Graphics graphics = Graphics.FromImage(path)
Building on Daniel's answer, but specifically for ASP.Net Core 2.2:
Use dependency injection in your controller:
A concrete instance of the IHostingEnvironment is injected into your controller, and you can use it to access WebRootPath (wwwroot).
It would be cleaner to inject an
IHostingEnvironment
and then either use itsWebRootPath
orWebRootFileProvider
properties.For example in a controller:
In a view you typically want to use
Url.Content("images/foo.png")
to get the url for that particular file. However if you need to access the physical path for some reason then you could follow the same approach:This works: