2017-04-13 84 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 –

相关问题