0

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Ratna Raju
  • 37
  • 12
  • ok, what's the problem? – manish Apr 13 '17 at 08:19
  • how to store that json object into a file? @manish – Ratna Raju Apr 13 '17 at 08:21
  • You want to [get the raw JSON](http://stackoverflow.com/questions/17822278/asp-net-mvc-read-raw-json-post-data) and [write the text to a file](https://msdn.microsoft.com/en-us/library/8bh11f1k.aspx). Hey presto! – Luke Apr 13 '17 at 08:23

1 Answers1

4

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);
}
Luke
  • 22,826
  • 31
  • 110
  • 193