2017-04-13 85 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" 
    } 
}); 

添加菜單是操作方法,菜單信息是JSON對象。

+0

OK,有什麼問題呢? – manish

+0

如何將json對象存儲到文件中? @manish –

+0

你想[獲取原始JSON](http://stackoverflow.com/questions/17822278/asp-net-mvc-read-raw-json-post-data)和[將文本寫入文件] (https://msdn.microsoft.com/en-us/library/8bh11f1k.aspx)。嘿presto! – Luke

回答

4

假設沒有其他要求以外從請求讀取JSON和更新包含在文件的JSON作爲問題要求:

[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

謝謝@Luke –

相關問題