将数据写入本地json文件的简单方法?

问题描述:

我现在正在为项目使用ASP.NET 5(vNext),但是在将数据写入本地文件时遇到了一些问题.我只是将要存储在公共wwwroot文件夹中的现有文件中的som JSON数据. JSON序列化等没问题,我只需要一些有关从ASP.NET 5 MVC6控制器写入本地文件的可用方法的建议.

I'm using ASP.NET 5 (vNext) for a project right now but I've been having some issues with writing data to a local file. I've simply got som JSON data I want to put in an existing file in the public wwwroot folder. The JSON serialization etc. is no problem I just need some advice about available methods of writing to local files from an ASP.NET 5 MVC6 controller.

我现在正在使用beta 7,使用streamwriter或File并没有运气.所有示例/建议将不胜感激.

I'm using beta 7 right now and I've had no luck using streamwriter or File. All examples/suggestions will be much appreciated.

使用 http://www.newtonsoft.com /json .

public T Read()
{
     return JsonConvert.DeserializeObject<T>(File.ReadAllText(_filePath));
}
public void Write(T model)
{
     File.WriteAllText(_filePath, JsonConvert.SerializeObject(model));
}

我在ASP.NET 5项目中使用了它,没关系

I used it in my ASP.NET 5 project and it was okay