Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

IVR: Phone Tree with C# and ASP.NET MVC

Download the Code

ET Phone Home: IVR C# Example

This ASP.NET MVC sample application is modeled after a typical call center experience with an IVR, but with more Reese's Pieces.

Stranded aliens can call a phone number and receive instructions on how to get out of earth safely, or call their home planet directly. In this tutorial, we'll show you the key bits of code to make this work.

To run this sample app yourself, download the code and follow the instructions on GitHub.

Read how Livestream and others built IVR with Twilio. Also, find sample code for many web languages and frameworks on our IVR application page.

Click here to start the tutorial!

Respond to the Phone Call

To initiate the phone tree, we need to configure one of our Twilio numbers to send our web application an HTTP request when we get an incoming call.

Click on one of your numbers and configure the Voice URL to point to our app. In our code the route will be /ivr/welcome.

IVR Webhook Configuration

If you don't already have a server configured to use as your webhook, ngrok is a great tool for testing webhooks locally.

With our Twilio number configured, we are prepared to respond to the Twilio request.

Respond to Twilio with TwiML

Respond to the Twilio request with TwiML

Our Twilio number is now configured to send HTTP requests to this controller on any incoming voice calls. Our app responds with TwiML to tell Twilio what to do in response to the message.

In this case we tell Twilio to Gather the input from the caller and we Say a welcome message.

Loading Code Sample...

      Respond with TwiML to gather an option from the caller


      After reading the text to the caller and retrieving their input, Twilio will send this input to our application.

      See where to send the caller's input

      Where to send the caller's input

      The gather's action parameter takes an absolute or relative URL as a value - in our case, the /menu/show route.

      When the caller has finished entering digits, Twilio will make a GET or POST request to this URL including a Digits parameter with the number our caller chose.

      After making this request, Twilio will continue the current call using the TwiML received in your response. Any TwiML verbs occuring after a <Gather> are unreachable, unless the caller enters no digits.

      Loading Code Sample...

          Send caller input to the intended route


          Now that we have told Twilio where to send the caller's input, we can look at how to process that input.

          Process the caller's selection

          The Main Menu: Process the caller's selection

          This route handles processing the caller's input.

          If our caller chooses 1 for directions, we use the helper method ReturnInstructions to respond with TwiML that will Say directions to our caller's extraction point.

          If the caller chooses 2 to call their home planet, we need to gather more input from them. We'll cover this in the next step.

          If the caller enters anything else we respond with a TwiML Redirect to the main menu.

          Loading Code Sample...

              Main menu and return instructions


              That was only the main menu and the first option. If the caller chooses 2, we will take them to The Planet Directory in order to collect more input.

              Get more input from your caller

              The Planet Directory: Collect more input from the caller

              If our callers choose to call their home planet we will give them the planet directory. This is akin to a typical "company directory" feature of most IVRs.

              In our TwiML response we again use a Gather verb to receive our caller's input. This time, the action verb points to the planets route, which will switch our response based on what the caller chooses.

              Loading Code Sample...

                  Collect more input from the caller via the Planet Directory


                  Again we show some options to the caller and instruct Twilio to collect the caller's choice.

                  See how we use the caller's input to call another number

                  The Planet Directory: Connect the caller to another number

                  In this route, we grab the caller's selection off the request and store it in a variable called userOption. We then use a Dial verb with the appropriate phone number to connect our caller to their home planet.

                  The current numbers are hardcoded, but they could also be read from a database or from a file.

                  Loading Code Sample...

                      Connect to another number based on caller input


                      That's it! We've just implemented an IVR phone tree that will delight and serve your customers.

                      What's next?

                      Where to Next?

                      If you're a C# developer working with Twilio, you might enjoy these other tutorials:

                      Automated Survey

                      Instantly collect structured data from your users with a survey conducted over a voice call or SMS text messages. Learn how to create your own survey in ASP.NET MVC.

                      Call Tracking

                      Convert web traffic into phone calls with the click of a button.

                      Did this help?

                      Thanks for checking out this tutorial! If you have any feedback to share with us, we'd love to hear it. Connect with us on Twitter and let us know what you build!

                      Agustin Camino Jennifer Aprahamian David Prothero Hector Ortega Andrew Baker Paul Kamp  Kat King Justin Pirie

                      Need some help?

                      We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.

                      Loading Code Sample...