繁体   English   中英

为什么我的 ASP.NET Core 2 方法不能返回 TwiML?

[英]Why can't my ASP.NET Core 2 method return TwiML?

我有一个 ASP.NET Core 2 应用程序,我正在尝试将对 Twilio 号码的呼叫转发到另一个号码。 当我尝试返回TwiML时,我收到一条错误消息:

“不可调用成员‘TwiML’不能像方法一样使用。”

这是方法:

[HttpPost]
public TwiMLResult ForwardCall(string called)
{
   var response = new VoiceResponse();
   response.Dial(newNumber);

   return TwiML(response);
}

错误发生在这里:

return TwiML(response);

我见过的所有代码示例都告诉我以这种方式返回TwiML ,但出于某种原因,我无法这样做。

问题是控制器没有从TwilioController继承。 一旦我的控制器继承自TwilioController,错误消失了。

可能更多.net nuget相关,但我不得不在visual studio识别Twilio.Aspnet命名空间之前卸载/重新安装twilio包。

如果您从 Azure Function 遇到此问题,并且您无法从 TwilioController 继承,那么类似这样的方法将起作用,毕竟 TwiML 只是 XML。

[FunctionName("ProcessCall")]
public static async Task<HttpResponseMessage> Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
    ILogger log)
{
    var voiceResponse = new VoiceResponse();

    voiceResponse.Dial(number: "<NumberGoesHere>");

    return new HttpResponseMessage
    {
        Content = new StringContent(voiceResponse.ToString(), System.Text.Encoding.UTF8, "application/xml")
    };
}

由于TwiML只是XML ,我会考虑返回一个ContentResult

ContentResult对象是一个ActionResult ,它返回一个简单的字符串。 它还支持设置将随响应返回的Content-Type

[HttpPost]
public ContentResult ForwardCall(string called)
{
   var response = new VoiceResponse();
   response.Dial(newNumber);

   return new ContentResult(TwiML(response), "text/xml");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM