Twilio First sample Program through asp.net not working

Viewed 71

I am very new to ASP.NET. I am attempting to configure a webhook for Twilio. I have created a default asp.net application with no real code changes from it's creation with the exception of adding in the Twilio helper libraries and Creating a controller based on their tutorial. I am using ngrok to tunnel in the http requests from Twilio. on the ngrok control panel it shows that a 'post/' came in when I send a text message from my phone. But the example is supposed to respond with "Hello World" I believe my app is receiving the http request but it is not responding. Below is the code I cut and pasted from the Twilio example. I am not sure how the http request is supposed to get to this controller. I believe that is the part I am missing. The public Class TwLHandler is showing 0 references.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Mvc;
using Twilio.AspNet.Common;
using Twilio.AspNet.Mvc;
using Twilio.TwiML;

namespace DAMTwilio.Controllers
{
    public class TwLHandler : TwilioController
    {
        [HttpPost]
        public TwiMLResult Index(SmsRequest request)
        {
            var response = new MessagingResponse();
            response.Message("Hello World");
            return TwiML(response);
        }
    }
}
1 Answers

TLDR: All controllers need to be suffixed with Controller, so in your case TwLHandler should be TwLController.

I was able to replicate your issue using ASP.NET MVC on .NET Framework. ASP.NET MVC uses a lot of conventions which makes things less explicit. Hence you are seeing that TwLHandler has 0 references.

ASP.NET MVC uses reflection to find your controllers so you don't have to explicitly reference them, as long as you follow the naming conventions. One of the naming conventions being, all controllers need to be suffixed with Controller, so in your case TwLHandler should be TwLController.

The default route in ASP.NET MVC is {controller}/{action}/{id}, first the name of the controller, then the name of the action, and then the id of whatever resource is being requested. This is configured in the RouteConfig.cs file:

    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }

Out of the box, the controller will default to "Home", the action to "Index", and the id is optional. That means for you, with the updated class name to TwLController, the route should be /TwL/Index or /TwL as you can omit the default Index name. (casing is ignored in the URL, so you don't have to match your controller name casing in the URL)

Please make these changes and let us know if there's any other issues.

Related