webapi post请求返回Null异常

时间:2016-11-12 04:22:15

标签: c# asp.net-web-api

我在Web API控制器中有一个简单的post方法,它接受HttpRequestMessage。不知何故,XML没有被解析为HttpRequestMessage。我已将应用配置为使用XmlSerializer

尝试将简单的XML传递给它但给我Null引用异常

public class SyncSubscriptionController : ApiController
{       
    [HttpPost]
    public HttpResponseMessage Post(HttpRequestMessage req)
    {    
        var data = req.Content.ReadAsStringAsync().Result;
    }
}

从Fiddler发送帖子请求:

User-Agent: Fiddler
Host: localhost:11033
Content-Length: 1852
Content-Type: text/xml

Body:
<customer>
<customer_id>1234</customer_id>
</customer>

Global.asax中

GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true;

0 个答案:

没有答案