我不熟悉网络api.我想使用web api接收HTTP POST数据.内容类型为application/x-www-form-urlencoded
,请求正文如下:
data={"mac":"0004ED123456","model":"SG6200NXL"}
(JSON格式).
我的控制器是这样的:
public void Post([FromBody]string formData)
{
data.Add(formData);
}
但是formData总是空的.只有当我将请求主体更改为:
={"mac":"0004ED123456","model":"SG6200NXL"}
我能找到{"mac":"0004ED123456","model":"SG6200NXL"}
个保存在我的formData
字符串中.
所以我的问题是,我应该如何接收以下格式的数据:
data={"mac":"0004ED123456","model":"SG6200NXL"}
?
有没有一种简单的方法可以将JSON淡化为C#?
谢谢你的帮助!
更新:
public class Device
{
public string mac { get; set; }
public string model { get; set; }
}
我的HTTP POST请求是:
标题:
User-Agent: Fiddler
Content-type: application/x-www-form-urlencoded
Host: localhost:52154
Content-Length: 46
正文:
data={"mac":"0004ED123456","model":"SG6200NX"}
据我所知,我必须使用Content-type: application/x-www-form-urlencoded
,因为HTTP POST是由路由发送的.我的工作是接收数据.