Ratna Raju Ratna Raju - 4 months ago 19
AngularJS Question

How to store and update JSON data into file using ASP.NET MVC?

I want to store and update POST json data into a file using ASP.NET MVC. I'm sending data:

url: "AddMenus",
dataType: 'json',
method: 'POST',
data: MenusInfo,
headers: {
"Content-Type": "application/json"

Add Menus is action method and MenusInfo is a JSON object.

Answer Source

Assuming no other requirements other than to read the JSON from the request and update the JSON contained in that file as requested in the question:

public ActionResult AddMenus()
    // Get the raw json
    Request.InputStream.Seek(0, SeekOrigin.Begin);
    string jsonData = new StreamReader(Request.InputStream).ReadToEnd();

    // Creates or overwrites the file with the contents of the JSON
    System.IO.File.WriteAllText(@"C:\textfile.txt", jsonData);