2017-07-14 59 views
0

为了简洁起见,我有一个端点这需要一些参数,并返回一个TwiML响应呼叫重定向到另一个端点,以收集用户的DTMF响应:网址安全行动

[HttpGet] 
public async MyEndpoint<ActionResult> Test(string parameterOne, string parameterTwo) 
{ 
    var response = new VoiceResponse() 
     .Redirect(
      url: $"https://example.com/Gather?parameterOne={parameterOne}&parameterTwo={parameterTwo}", 
      method: "POST"); 

    return TwiML(response); 
} 

产生以下XML:

<?xml version="1.0" encoding="utf-8"?> 
<Response> 
    <Redirect method="POST">https://example.com/Gather?paramaterOne="test"&amp;parameterTwo="test"</Redirect> 
</Response> 

的想法是,Twilio应该张贴它是标准的请求对象这个端点,但包括我的查询字符串参数:

[HttpPost] 
public async Task<ActionResult> Gather([FromUri] string parameterOne, string parameterTwo [FromBody] TwilioCallbackRequest request) 
{ 
    // Do stuff 
} 

但是,由于XML编码的&amp;不被Web服务器理解为查询字符串分隔符,所以请求失败。

理想情况下,将有一种方法来修改Twilio请求的表单值以包含我需要传递的参数,但我不相信该功能存在。

我的问题是:

A.有没有什么办法让Twilio到URL编码它的查询?或者

B.什么是接受在这个畸形的查询字符串的最佳方式在ASP.NET有没有机会处理请求之前没有错误的方式?

目前我的哈克解决方法是发送我的查询字符串参数作为一个大的下划线分隔的字符串,并从它手动解析我的参数,但我怀疑这是最好的方法。我也考虑过使用OWIN中间件拦截请求并尝试修改此端点的查询字符串。

回答

1

Twilio开发传播者在这里...

它看起来像你同时使用Twilio和Twilio.AspNet.Mvc包来回报您的回应。您也可以通过在不使用Twilio.AspNet.Mvc软件包的情况下准备Get方法的响应来实现您的目标。这里有一个例子:

[HttpGet] 
public HttpResponseMessage Get(string parameterOne, string parameterTwo) 
{ 
    var response = new VoiceResponse() 
     .Redirect(
      url: $"https://example.com/Gather?paramaterOne={parameterOne}&parameterTwo={parameterTwo}", 
     method: "POST"); 
    var content = new StringContent(response.ToString(), Encoding.UTF8, "application/xml"); 
    return new HttpResponseMessage 
    { 
     Content = content, 
     StatusCode = HttpStatusCode.OK 
    }; 
} 

试试这个,让我知道你是否遇到问题。

+0

感谢您的建议,但这返回如下: '的StatusCode:200,ReasonPhrase: 'OK',版本:1.1,内容:System.Net.Http.StringContent,头: { 的Content-Type:应用/ XML; charset = utf-8 }' Twilio不喜欢(12100文档解析失败) –

+0

非常有趣的是,你得到了这个回应。您是否介意张贴(或通过电子邮件发送给我[email protected])两种方法使用的确切代码? 基于上述回应,我怀疑字符串内容并未实际发送。 –

+0

你是对的!我有MVC和WebAPI绑定为相同的路线而战。但值得一提的是,Twilio XML生成器('TwiML'构造函数)在返回XML时可能不应该混淆和号。 –