Given the error you receive, it seems to me that you are trying to use _env.WebRootPath + "/images/textfile.txt" in your views, for example, as an href attribute.
- Doing this in a browser will try to ask your page for a URL that looks like
file:///C:/Users/PathToYourSite/wwwroot/images/textfile.txt that ends with the error you are experiencing. You can learn more about this in this answer , but in browsers such as the Chrome modem, http protocol requests for the file protocol are disabled by default.
Therefore, the IHostingEnvironment approach should be used when you want to read the contents of a file as part of the server-side logic so that you can do something with them.
However, if you need to create links to your files in wwwroot and use them in your submissions, you have several options:
Use IUrlHelper to resolve the shared URL for the file inside wwwroot, where "~/" is the wwwroot folder. Then use it when you need it, like href:
@Url.Content("~/images/textfile.txt")
Directly create a razor binding with the href attribute, starting with "~/" . (You even get intellisense there)
<a href="~/images/textfile.txt">Get file from wwwroot</a>
source share