我收到了这样的请求。
POST /API/Event?EventID=15&UserID=1&Severity=5&DeptID=1&FlowTag=CE HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: localhost:8088
Content-Length: 9
Expect: 100-continue
Connection: Keep-Alive
HTTP/1.1 100 Continue
Desc=test
我的WebAPI界面是这样的:
[Route("API/Event"), HttpPost]
public IHttpActionResult StationCreateWorkItem(long EventID, long UserID, int Severity,
long DeptID, string FlowTag, [FromBody] string Desc)
但是,我的Desc参数始终为NULL。如果我无法在WebAPI(OWIN)中使用[FromBody],我可以知道如何检索正文内容
抱歉,我无法更改收到的消息,因为它是由另一家公司开发的。
答案 0 :(得分:7)
默认情况下,Web API使用以下规则绑定参数:
public HttpResponseMessage Post([FromBody] int id,[FromBody] 字符串名称){...}
http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api
https://blogs.msdn.microsoft.com/jmstall/2012/04/16/how-webapi-does-parameter-binding/
现在的解决方案
POST /API/Event?EventID=15&UserID=1&Severity=5&DeptID=1&FlowTag=CE HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: localhost:8088
Content-Length: 9
Expect: 100-continue
Connection: Keep-Alive
HTTP/1.1 100 Continue
=test
删除了描述
答案 1 :(得分:0)
最后,您可能想要使用以下内容:
[Route("valueparam/{value}"), HttpGet]
public void PostWithParam([FromUri]string value)
{
}
result = client.GetAsync(baseAddress + "api/valueparam/1").Result;
和
[Route("valueparamfrombody"), HttpPost]
public void PostWithParam2([FromBody] JObject value1)
{
}
var req = new
{
value1 = "1"
};
var obj = JsonConvert.SerializeObject(req);
content = new StringContent(obj);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
result = client.PostAsync(baseAddress + "api/valueparamfrombody", content).Result;
答案 2 :(得分:0)
在我的 case 中,我的模型有一个 Guid?
属性,我发送了一个空字符串,因此解析失败并得到 null
。