【问题标题】:Return XML content on WebAPI OWIN for Twilio在 WebAPI OWIN for Twilio 上返回 XML 内容
【发布时间】:2014-09-06 12:54:24
【问题描述】:

我正在使用 OWIN/Katana 自托管在 Azure Worker 角色中制作一个非常简单的 WebAPI。从主机的角度来看,一切都很好,因为我收到了请求并且它被路由到我的 Action 就好了。

问题是该操作必须为 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/">&lt;Response&gt;
  &lt;Say&gt;Hello World&lt;/Say&gt;
&lt;/Response&gt;</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,似乎是错误的编码......

我尝试在 text/xml 的请求中添加 Content-Type 和 Accept 标头(我不能使用 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);
    }
}

不走运……

任何帮助将不胜感激。

谢谢!

【问题讨论】:

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


    【解决方案1】:

    我知道这已经得到解答,但答案需要将您的响应类型从 IHttpActionResult 更改为 HttpResponeMessage。 如果您想保留 IHttpActionResult(并使其更简洁),您可以这样做:

    using System.Xml.Linq;
    
    [HttpGet]
    [Route("menu")]
    public IHttpActionResult Menu(string from, string to, string callSid) 
    {
         var response = new TwilioResponse();
         response.Say("Hello World");
    
         return Ok(XElement.Parse(response.ToString()));
    }
    

    在您的 WebApiConfig 中,将其添加到注册表中。 它将导致 XML 成为默认值。 为什么 Twilio 不包含 application/xml 作为接受标头是我无法理解的。

      config.Formatters.Clear();
      config.Formatters.Add(new XmlMediaTypeFormatter());
      config.Formatters.Add(new JsonMediaTypeFormatter());
      config.Formatters.Add(new FormUrlEncodedMediaTypeFormatter());
    

    【讨论】:

      【解决方案2】:
      [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"
              )
          };
      }
      

      【讨论】:

        猜你喜欢
        • 2015-07-02
        • 2012-07-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多