Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 返回用于Twilio的WebAPI OWIN上的XML内容_C#_Azure_Asp.net Web Api_Owin - Fatal编程技术网

C# 返回用于Twilio的WebAPI OWIN上的XML内容

C# 返回用于Twilio的WebAPI OWIN上的XML内容,c#,azure,asp.net-web-api,owin,C#,Azure,Asp.net Web Api,Owin,我正在使用OWIN/Katana以Azure Worker角色自托管,制作一个非常简单的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

我正在使用OWIN/Katana以Azure Worker角色自托管,制作一个非常简单的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/">&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());
        }
不走运

任何帮助都将不胜感激


谢谢

我知道这已经得到了回答,但回答需要将您的响应类型从IHttpActionResult更改为HttpResponeMessage。
[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"
        )
    };
}
如果要保留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作为accept头

  config.Formatters.Clear();
  config.Formatters.Add(new XmlMediaTypeFormatter());
  config.Formatters.Add(new JsonMediaTypeFormatter());
  config.Formatters.Add(new FormUrlEncodedMediaTypeFormatter());
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()));
}
  config.Formatters.Clear();
  config.Formatters.Add(new XmlMediaTypeFormatter());
  config.Formatters.Add(new JsonMediaTypeFormatter());
  config.Formatters.Add(new FormUrlEncodedMediaTypeFormatter());