Read solution data files ASP.Net Core
I have an ASP.NET Core (1.0-rc1-final) MVC solution and I wish to store a simple text file within the project which contain a list of strings which I read into a string array in my controller.
Where should I store this file in my project and how do I read those files in my controllers?
In ASP.net 4.x I'd have used the app_data
folder and done something like this
string path = Server.MapPath("~/App_Data/File.txt");
string[] lines = System.IO.File.ReadAllLines(path);
But Server.MapPath
does not seem to be valid in ASP.Net Core 1 and I'm not sure that the app_data
folder is either.
I found a simple solution to this.
Firstly, you can create a folder anywhere in your solution, you do not have to stick to the conventions like 'app_data' from .net 4.x.
In my scenario, I created a folder called 'data' at the root of my project, I put my txt file in there and used this code to read the contents to a string array
var owners = System.IO.File.ReadAllLines(@"..\data\Owners.txt");
You can get the enviroment with Dependency Injection in your controller:
using Microsoft.AspNetCore.Hosting;
....
public class HomeController: Controller
{
private IHostingEnvironment _env;
public HomeController(IHostingEnvironment env)
{
_env = env;
}
...
Then you can get the wwwroot location in your actions: _env.WebRootPath
var owners = System.IO.File.ReadAllLines(System.IO.Path.Combine(_env.WebRootPath,"File.txt"));
in your controller you could take a dependency on IApplicationEnvironment and have it injected into the constructor then you can use it to establish the path to your file so your file can live in a folder within the project. In the example below "env" is the instance of IApplicationEnvironment
using Microsoft.Extensions.PlatformAbstractions;
var pathToFile = env.ApplicationBasePath
+ Path.DirectorySeparatorChar.ToString()
+ "yourfolder"
+ Path.DirectorySeparatorChar.ToString()
+ "yourfilename.txt";
string fileContent;
using (StreamReader reader = File.OpenText(pathToFile))
{
fileContent = reader.ReadToEnd();
}
ApplicationBasePath represents the applicationRootFolder
note that there also exists IHostingEnvironment which has the familiar .MapPath method, but it is for things stored below the wwwroot folder. You should only store things below the wwwroot folder that you want to serve with http requests so it is better to keep your list of strings in a different folder.
This has always worked for me locally and on IIS.
AppDomain.CurrentDomain.BaseDirectory
To access your file, you could simply do the following:
import System.IO
...
var owners = File.ReadAllLines(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "File.txt"))
IApplicationEnvironment
and IRuntimeEnvironment
have been removed as of an announcement on github on 2016/04/26.
I replaced @JoeAudette's code with this
private readonly string pathToFile;
public UsersController(IHostingEnvironment env)
{
pathToFile = env.ContentRootPath
+ Path.DirectorySeparatorChar.ToString()
+ "Data"
+ Path.DirectorySeparatorChar.ToString()
+ "users.json";
}
Where my .json
file is located at src/WebApplication/Data/users.json
I then read/parse this data like so
private async Task<IEnumerable<User>> GetDataSet()
{
string source = "";
using (StreamReader SourceReader = File.OpenText(pathToFile))
{
source = await SourceReader.ReadToEndAsync();
}
return await Task.FromResult(JsonConvert.DeserializeObject<IEnumerable<User>>(source)));
}