模型总是空的XML POST [英] Model always null on XML POST
问题描述
我目前工作的系统之间的集成,我已经决定使用的WebAPI它,但我快成为一个问题...
I'm currently working on an integration between systems and I've decided to use WebApi for it, but I'm running into an issue...
让我们说我有一个模型:
Let's say I have a model:
public class TestModel
{
public string Output { get; set; }
}
和POST方法是:
public string Post(TestModel model)
{
return model.Output;
}
我创建提琴手的请求与头:
I create a request from Fiddler with the header:
User-Agent: Fiddler
Content-Type: "application/xml"
Accept: "application/xml"
Host: localhost:8616
Content-Length: 57
和身体:
<TestModel><Output>Sito</Output></TestModel>
在方法发表
的模式
参数始终是空
,我不知道为什么。
有没有人有一个线索?
The model
parameter in the method Post
is always null
and I have no idea why.
Does anyone have a clue?
推荐答案
两件事情:
-
您不必报价
,
周围的内容类型,并接受提琴手头值:
You don't need quotes
""
around the content type and accept header values in Fiddler:
User-Agent: Fiddler
Content-Type: application/xml
Accept: application/xml
网络API使用的DataContractSerializer
默认情况下,XML序列化。所以,你需要包括你的类型的命名空间中的XML:
Web API uses the DataContractSerializer
by default for xml serialization. So you need to include your type's namespace in your xml:
<TestModel
xmlns="http://schemas.datacontract.org/2004/07/YourMvcApp.YourNameSpace">
<Output>Sito</Output>
</TestModel>
或者你也可以配置Web API来使用的XmlSerializer
在 WebApiConfig.Register
:
config.Formatters.XmlFormatter.UseXmlSerializer = true;
那么你不需要命名空间中的XML数据:
Then you don't need the namespace in your XML data:
<TestModel><Output>Sito</Output></TestModel>
这篇关于模型总是空的XML POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!