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.

3
  • ok, what's the problem? Commented Apr 13, 2017 at 8:19
  • how to store that json object into a file? @manish Commented Apr 13, 2017 at 8:21
  • You want to get the raw JSON and write the text to a file. Hey presto! Commented Apr 13, 2017 at 8:23

1 Answer 1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.