2017-04-13 10 views
0

ASP.NET MVCを使用してPOST jsonデータをファイルに保存して更新します。私はデータを送信しています:ASP.NET MVCを使用してJSONデータをファイルに保存および更新する方法は?

$http({ 
    url: "AddMenus", 
    dataType: 'json', 
    method: 'POST', 
    data: MenusInfo, 
    headers: { 
      "Content-Type": "application/json" 
    } 
}); 

Menusはアクションメソッドで、MenusInfoはJSONオブジェクトです。リクエストからJSONを読み、質問に要求されるように、そのファイルに含まれているJSONを更新する以外の他の要件と仮定していない

+0

をOK、問題は何ですか? – manish

+0

jsonオブジェクトをファイルに保存する方法は? @manish –

+0

[raw JSONを取得する](http://stackoverflow.com/questions/17822278/asp-net-mvc-read-raw-json-post-data)と[テキストをファイルに書き込む] (https://msdn.microsoft.com/en-us/library/8bh11f1k.aspx)。こんにちは! – Luke

答えて

4

[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); 
} 
+1

ありがとう@ルーク –

関連する問題