我正在尝试使用Twilio完成一个示例短信应用程序,我向我的Twilio号码发送短信,Twilio服务给我回复。我知道Twilio服务正在接触我的API,因为我可以看到来自Twilio的传入请求到达我的API,我可以看到我的API的响应,但我认为有些错误,因为我从来没有收到过短信回复。
[HttpPost]
[Route("EchoTest")]
public IHttpActionResult EchoTest()
{
string response = "<Response><Sms>Thanks for the message</Sms></Response>";
return ResponseMessage(Request.CreateResponse(HttpStatusCode.OK, response, new XmlMediaTypeFormatter()));
}
我将返回ResponseMessage
,因此我可以保持一致IHttpActionResult
。我也试过返回HttpResponseMessage
,如下所示,结果相同。
[HttpPost]
[Route("EchoTest")]
public HttpResponseMessage EchoTest()
{
string response = "<Response><Sms>Thanks for the message</Sms></Response>";
Request.CreateResponse(HttpStatusCode.OK, response, new XmlMediaTypeFormatter());
}
这是我的API发回的内容......
<string
xmlns="http://schemas.microsoft.com/2003/10/Serialization/"><Response><Sms>Thanks for the message</Sms></Response>
</string>
我搞砸了XML响应吗?我希望寄回Twilio的是......
<Response><Sms>Thanks for the message</Sms></Response>
答案 0 :(得分:3)
参见网页:https://msdn.microsoft.com/en-us/library/hh969056(v=vs.118).aspx 试试这个
XElement response = XElement.Parse("<Response><Sms>Thanks for the message</Sms></Response>");
Request.CreateResponse<XElement>(request, HttpStatusCode.OK, response);