Ratna Raju Ratna Raju - 8 months ago 34
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:

$http({
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:

[HttpPost]
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);
}