Get file path in .net core from wwwroot folder

This is how;

public class HomeController : Controller {
    private IWebHostEnvironment _hostEnvironment;

    public HomeController(IWebHostEnvironment environment) {
        _hostEnvironment = environment;
    }

    [HttpGet]
    public IActionResult Get() {
        string path = Path.Combine(_hostEnvironment.WebRootPath, "Sample.PNG");
        return View();
    }
}

References

https://weblog.west-wind.com/posts/2020/Feb/26/Working-with-IWebHostEnvironment-and-IHostingEnvironment-in-dual-targeted-NET-Core-Projects#out-with-old-in-with-the-new-iwebhostenvironment

FavoriteLoadingAdd to favorites
Spread the love

Author: Shahzad Khan

Software developer / Architect