2017-07-14 66 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時可能不應該混淆和號。 –