我正在使用 Azure Worker 角色中自托管的 OWIN/Katana 制作一个非常简单的 WebAPI。从主机的角度来看,一切都很好,因为我收到了请求,并且它很好地路由到了我的操作。
问题在于该操作必须为 API 调用者/调用者返回 XML,并且返回错误的编码字符串,如下所示:
RAW Response:
HTTP/1.1 200 OK
Content-Length: 150
Content-Type: text/xml; charset=utf-8
Server: Microsoft-HTTPAPI/2.0
Date: Wed, 16 Jul 2014 05:49:42 GMT
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/"><Response>
<Say>Hello World</Say>
</Response></string>
RAW Request:
GET http://localhost:81/v1/ivr/menu?from=+12345&to=645645&callsid=11111111 HTTP/1.1
User-Agent: Fiddler
Host: localhost:81
Content-Type: text/xml;
response.ToString() result:
<Response>
<Say>Hello World</Say>
</Response>
API Controller Action code:
[HttpGet]
[Route("menu")]
public IHttpActionResult Menu(string from, string to, string callSid)
{
var response = new TwilioResponse();
response.Say("Hello World");
return Ok(response.ToString());
}
好吧,我所需要的只是返回 XML,如下所示:
<Response>
<Say>Hello World</Say>
</Response>
我究竟做错了什么?响应来自标签之间,并带有一个奇怪的 xmlns,其编码似乎是错误的...
我尝试在请求上添加 Content-Type 和 Accept header 到 text/xml(我不能使用 application/xml,但即使我这样做,它也不会更改输出响应)...
我还尝试创建一个 OWINMiddleware,将响应内容类型强制为 text/xml,如下所示:
public class XmlResponseMiddleware : OwinMiddleware
{
public XmlResponseMiddleware(OwinMiddleware next) : base(next)
{
}
public override async Task Invoke(IOwinContext context)
{
context.Response.ContentType = "text/xml";
await this.Next.Invoke(context);
}
}
没有运气...
如有任何帮助,我们将不胜感激。
谢谢!
最佳答案
[HttpGet]
[Route("menu")]
public HttpResponeMessage Menu(string from, string to, string callSid)
{
var response = new TwilioResponse();
response.Say("Hello World");
return new HttpResponseMessage()
{
Content = new StringContent(
response.ToString(),
Encoding.UTF8,
"text/xml"
)
};
}
关于c# - 在 Twilio 的 WebAPI OWIN 上返回 XML 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24773266/