简体   繁体   English

当HTTP-POST具有主体时,url参数为null

[英]When HTTP-POST has body, url parameter is null

This is self-hosted RESTful MVC4 Web API, the only route is api/{controller}/{state} . 这是自托管的RESTful MVC4 Web API,唯一的路由是api/{controller}/{state} When I send an HTTP-POST that has a body, the state argument comes in null. 当我发送具有主体的HTTP-POST时,state参数为null。 If I remove the body the state variable is present. 如果删除主体,则存在状态变量。

The way I thought it worked for HTTP-POST was that the url parameters get mapped then the body gets serialized into the extra parameter, which in this case is data parameter. 我认为它适用于HTTP-POST的方式是先映射url参数,然后将正文序列化为extra参数,在本例中为data参数。 The content is just string data which I had to write a custom MediaTypeFormatter (which I thought was odd it couldn't handle a regular string). 内容只是字符串数据,我必须编写一个自定义MediaTypeFormatter(我认为奇怪的是它无法处理常规字符串)。

Here is my controller signature 这是我的控制器签名

public class MyController : ApiController
{
    public void Post(string state, string data)
    {
    }
}

Has anyone seen this behavior before or can explain to me why having a body present is affecting my url parameter? 有没有人以前见过这种行为,或者可以向我解释为什么存在身体会影响我的网址参数?

One Solution: I tried changing data parameter into a complex type (Just a class with a public property) and sending the content as text/xml instead of text/plain and it worked as expected. 一种解决方案:我尝试将数据参数更改为复杂类型(只是具有公共属性的类),然后将内容作为text / xml而不是text / plain发送,并且按预期方式工作。 The state parameter wasn't null and I had my strongly typed object with the data. state参数不为null,并且我有带有数据的强类型对象。 I suppose MVC wants to have something to deserialize like XML or JSON for the http-request body... 我想MVC希望对HTTP请求正文进行反序列化,例如XML或JSON ...

More Research: I've had the chance to run some more tests. 更多研究:我有机会进行更多测试。 If the body of a post is XML/JSON it will first try to map the properties of the body-object to the method parameters like so. 如果帖子的主体是XML / JSON,它将首先尝试将主体对象的属性映射到方法参数,如下所示。 If still has unmapped properties then it will match the remaining properties to the properties of any strongly-typed objects in the method parameters 如果仍然具有未映射的属性,则它将剩余的属性与方法参数中任何强类型对象的属性进行匹配

PostMethod(string p1, string p2, myClass obj) // if myClass has a p3 property it will be mapped from the xml body.
{
}

// xml in body of http-post
<Xml>
   </p1>
   </p2>
   </p3>
</Xml>

If all the parameters were not mapped, then it will attempt to map the url parameters. 如果未映射所有参数,则它将尝试映射url参数。 To relate it directly to my initial problem. 直接将其与我最初的问题联系起来。 The best and easiest solution I see at this time is to send text/xml like this. 我目前看到的最好,最简单的解决方案是像这样发送text / xml。

PostMethod(string state, string data)
{
}

<data>put data here</data>

Urlencoded key/value pairs also work very well. Urlencoded键/值对也可以很好地工作。

var r = client.PostAsync(url, new StringContent("data=Something", Encoding.UTF8, "application/x-www-form-urlencoded"));

My best guess is that the key/value nature of JSON and XML, FormEncoded help it to map to parameters so that is why it doesn't like plain strings. 我最好的猜测是,JSON和XML的键/值性质FormFormcoded帮助它映射到参数,因此这就是为什么它不喜欢纯字符串的原因。

This sure gave me a headache and I find the MVC4 documentation to be rather scarce (its still in beta), but I hope this can help someone else who may have the same problem. 这肯定让我头疼,我发现MVC4文档非常稀缺(仍处于beta版),但是我希望这可以帮助其他可能遇到相同问题的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM