当HTTP-POST身体,URL参数为null [英] When HTTP-POST has body, url parameter is null

查看:948
本文介绍了当HTTP-POST身体,URL参数为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是自托管的RESTful MVC4的Web API,唯一的途径是 API / {控制器} / {状态} 。当我发送一个HTTP-POST,有一个身体,状态参数进来空。如果我清除体内的状态变量是present。

This is self-hosted RESTful MVC4 Web API, the only route is api/{controller}/{state}. When I send an HTTP-POST that has a body, the state argument comes in null. If I remove the body the state variable is present.

我想它的工作对HTTP-POST方式是,URL参数获取映射然后身体被序列化到额外的参数,在这种情况下的数据参数。内容是我不得不写一个自定义MediaTypeFormatter(我认为是奇怪的无法处理的一个普通字符串)只是字符串数据。

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. 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).

下面是我的控制器的签名

Here is my controller signature

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

有没有人见过这种行为之前,或者为什么具有主体present是影响我的网址参数可以给我解释一下吗?

Has anyone seen this behavior before or can explain to me why having a body present is affecting my url parameter?

推荐答案

一个解决办法:
我试图改变数据参数到一个复杂的类型(只是一个具有公共财产类),并发送内容为text / xml的,而不是text / plain的,它工作正常。状态参数是不是null,我有我的数据强类型的对象。我想MVC希望得到的东西反序列化,如XML或JSON用于HTTP请求的身体...

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. The state parameter wasn't null and I had my strongly typed object with the data. I suppose MVC wants to have something to deserialize like XML or JSON for the http-request body...

更多的研究:
我已经运行一些测试的机会。如果一个职位的身体是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. 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>

如果所有的参数都没有映射,那么它会尝试将URL参数映射。要直接关系到我的最初的问题。最好的和最简单的解决方案,我在这个时候看到的是发送文字/ XML这样的。

If all the parameters were not mapped, then it will attempt to map the url parameters. 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.

PostMethod(string state, string data)
{
}

<data>put data here</data>

Urlen codeD键/值对也工作得很好。

Urlencoded key/value pairs also work very well.

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

我最好的猜测是JSON和XML的键/值性质,福曼codeD帮助它映射到参数,所以这就是为什么它不象普通的字符串。

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.

这肯定让我头疼,我觉得MVC4文档是相当稀缺的(它仍处于测试阶段),但我希望这可以帮助别人谁可能有同样的问题。

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.

这篇关于当HTTP-POST身体,URL参数为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆