在常规模型视图中呈现内容

时间:2013-12-17 05:22:10

标签: c# asp.net-mvc twilio

我正在关注twilio教程,他们有一个文件hello-monkey.cshtml的内容如下:

@{
    // make a list of senders we know, indexed by phone number
    ****var people = new Dictionary<string, string>() { 
        {"+14158675309","Curious George"},
        {"+14158675310","Boots"},
        {"+14158675311","Virgil"},
        {"+14158675312","Marcel"}
    };

    // if the caller is known, then greet them by name
    // otherwise, consider them just another monkey
    string name = "Monkey";
    if (!string.IsNullOrEmpty(Request["From"]))
    {
        name = people[Request["From"]];
    }**

    // now greet the caller
    Response.ContentType = "text/xml";**
}
<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Say>Hello @name.</Say>
</Response>

他们希望我们将页面呈现为http://www.mysite.com/hello-monkey.cshtml ...

如何在服务器端编写此代码..在模型部分,以便我可以将页面调用为http://www.mysite.com/Monkey

换句话说,是否可以在模型部分中传输粗体部分代码?

1 个答案:

答案 0 :(得分:1)

Twilio传道者在这里。

我不完全确定“模型部分”是什么意思,但是如果您不想将它放在视图中,您可以直接在Controller中执行上面显示的所有操作:

public class PhoneController : TwilioController  
   public ActionResult Hello() {

      var response = new TwilioResponse();
      response.Say(name);

      return TwiML(response);    
   }
}

我建议将Twilio.Mvc帮助程序库安装到您的ASp.NET MVC项目中以获取TwilioController基类。这使您可以使用上面显示的TwiML帮助器方法将TwiML命令作为ActionResult返回。

希望有所帮助。