Twilio 在 ASP.NET-MVC 中尝试创建可用的 VoIP phone 失败,因为网站上只有 XML 可见,而不是 phone

Attempt to create usable VoIP phone by Twilio in ASP.NET-MVC fails as Only XML is visible on the website instead of phone

我在 ASP.NET-MVC 中搜索 VoIP 技术很长时间了。我唯一找到的是 Twilio。

我创建了 ASP.NET-MVC 5.2 应用程序并添加了对以下内容的引用:

我把_Layout.csthml减到最小:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
</head>
<body>
        @RenderBody()
</body>
</html>

我找到了这个示例:https://www.twilio.com/docs/howto/phonemenu 我相信它应该会在我的网站上生成 phone 菜单。

我将操作添加到 HomeController

控制器的动作:

 public ActionResult PhoneMenu() {
        return View();
    }

我已经从 https://www.twilio.com/resources/tarball/phonemenu.zip 下载了 how-to 的内容并将其粘贴到:PhoneMenu.csthml 然后将其添加到我的项目中的 Views/Home/

@{

    @*@start snippet*@
    @* Define Menu *@
    Dictionary<string, string[]> web = new Dictionary<string, string[]>() {
        {"default",new string[] {"receptionist","hours", "location", "duck"} },
        {"location",new string[] {"receptionist","east-bay", "san-jose", "marin"} }
    };

    Uri cleanedUri = new Uri(Request.Url.GetComponents(UriComponents.AbsoluteUri & ~UriComponents.Port, UriFormat.UriEscaped));
    string baseUri = cleanedUri.AbsoluteUri.Remove(cleanedUri.AbsoluteUri.Length - cleanedUri.Segments.Last().Length);

    @* Get the menu node, index, and url *@
    string node = Request["node"] != null ? Request["node"] : "";
    int index = Request["Digits"] != null ? int.Parse(Request["Digits"]) : 0;

    string url = string.Format("{0}phonemenu.cshtml", baseUri);

    @* Check to make sure index is valid *@
    string destination = string.Empty;
    if (web.Keys.Contains(node) && web[node].Length >= index && Request["Digits"] != null) {
        destination = web[node][index];
    } else {
        destination = string.Empty;
    }
    @*end snippet*@

    @*start snippet*@
    @* Render TwiML *@
    Response.ContentType = "text/xml";
    var twiml = new Twilio.TwiML.TwilioResponse();

    switch (destination) {
        case "hours":
            twiml.Say("Initech is open Monday through Friday, 9am to 5pm");
            twiml.Say("Saturday, 10am to 3pm and closed on Sundays");
            break;
        case "location":
            twiml.Say("Initech is located at 101 4th St in San Francisco California");
            twiml.BeginGather(new { action = "phonemenu.cshtml?node=location", numDigits = "1" })
                .Say("For directions from the East Bay, press 1")
                .Say("For directions from San Jose, press 2");
            twiml.EndGather();
            break;
        case "east-bay":
            twiml.Say("Take BART towards San Francisco / Milbrae. Get off on Powell Street. Walk a block down 4th street");
            break;
        case "san-jose":
            twiml.Say("Take Cal Train to the Milbrae BART station. Take any Bart train to Powell Street");
            break;
        case "duck":
            twiml.Play("duck.mp3");
            break;
        case "receptionist":
            twiml.Say("Please wait while we connect you");
            twiml.Dial("NNNNNNNNNN");
            break;
        default:
            twiml.BeginGather(new { action = "phonemenu.cshtml?node=default", numDigits = "1" })
                .Say("Hello and welcome to the Initech Phone Menu")
                .Say("For business hours, press 1")
                .Say("For directions, press 2")
                .Say("To hear a duck quack, press 3")
                .Say("To speak to a receptionist, press 0");
            twiml.EndGather();
            break;
    }
    @*end snippet*@

    @*start snippet*@
    if (destination != String.Empty && destination != "receptionist") {
        twiml.Pause();
        twiml.Say("Main Menu");
        twiml.Redirect(url);
    }
    @*end snippet*@
}
@Html.Raw(twiml.ToString())

当我转到 url 时,我唯一看到的是:http://localhost:43281/Home/PhoneMenu 是生成的 xml 而不是 phone 菜单:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <link href="/Content/bootstrap.css" rel="stylesheet"/>
    <link href="/Content/site.css" rel="stylesheet"/>    
    <script src="/Scripts/modernizr-2.6.2.js"></script>    
</head>
<body>



<Response>
  <Gather action="phonemenu.cshtml?node=default" numDigits="1">
    <Say>Hello and welcome to the Initech Phone Menu</Say>
    <Say>For business hours, press 1</Say>
    <Say>For directions, press 2</Say>
    <Say>To hear a duck quack, press 3</Say>
    <Say>To speak to a receptionist, press 0</Say>
  </Gather>
</Response>    
</body>
</html>

可见结果:

问题:如何生成可用的phone?由于我找不到任何来源如何做到这一点,如果有比 Twilio 更简单的替代方案,那就太好了。

Twilio TwiML 生成不需要进入您的 Razor 视图,您只需使用控制器即可。所以你可能有类似下面的东西:

public ActionResult PhoneMenu() {
    var response = new TwimlResponse().Say("Hello"):
    return new TwimlResult(response);
}

或者,作为 Twilio.Mvc 的一部分,您可以选择从名为 TwilioController 继承的替代控制器,这样您就可以执行如下操作:

public class PhoneMenuController : TwilioController
{
    public ActionResult PhoneMenu()
    {
        var response = new TwimlResponse().Say("Hello");
        return Twiml(response);
    }
}

完整文档可在 GitHub https://github.com/twilio/twilio-csharp/

上找到