【发布时间】:2017-09-08 14:57:05
【问题描述】:
我有一个发布到我的 ASP.NET MVC 4 HttpPost 方法的移动应用程序,每次它接收到数据时,它都没有接收到正确的数据。
[HttpPost]
public void NewItems(Dictionary<string, List<Dictionary<string, string>>> newItemsDictionary)
{
}
我的 RouteConfig 设置如下:
routes.MapRoute(
name: "Default",
url: "{action}",
defaults: new { controller = "API", action = "Index" }
);
移动应用发布到 URL https://api.test.com/NewItems,参数是一个 JSON 编码的字典,有一个键 newItems,值是一个字典数组 ([["user": "testUser1", "itemNumber": "123-45678"], ["user": "testUser2", "itemNumber": "456-7890"]])
我在 POST 方法中设置了一个 try/catch 块来捕获任何错误。当方法被发布到时,它会进入 catch 块并告诉我 newItemsDictionary 参数中不存在“newItem”键。参数中确实存在的键是:action 和 controller。操作和控制器键不包含任何值。
为什么会发生这种情况,我应该改变什么来接收正确的数据?
【问题讨论】:
-
您的 NewItems 参数非常混乱。您需要将 javascript 对象结构与参数匹配,然后您可以在对象上调用 json.stringify 以序列化为 json。
标签: c# asp.net json asp.net-mvc asp.net-mvc-4