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.
I'm using beta 7 right now and I've had no luck using streamwriter or File. All examples/suggestions will be much appreciated.
Use 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));
}
I used it in my ASP.NET 5 project and it was okay
You use the abstract TextWriter
class in conjunction with the StreamWriter class
e.g.
TextWriter writer;
using (writer = new StreamWriter(@"C:\SampleLog.json", append: false))
{
writer.WriteLine(yourJsonString);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With