What is the best way to read in a text file from the server in asp.net-mvc
Solution 1:
If the file should not be directly available via URL, you should put it in App_Data.
For reading it, just use:
var fileContents = System.IO.File.ReadAllText(Server.MapPath(@"~/App_Data/file.txt"));
Solution 2:
Ok this way it works for me (VS2017)
- Set the Build Action of the file.txt to Content
- Check if Copy to output directory is not set to 'Do not copy'
-
Use
HostingEnvironment.MapPath(@"~/App_Data/file.txt")
(thanks to Hong comment)var fileContents = System.IO.File.ReadAllText(HostingEnvironment.MapPath(@"~/App_Data/file.txt"));