Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you implement TwiML as a method using the .NET Core MVC Framework?

I'm executing code based on a Twilio example for receiving and responding to SMS messages found here: https://www.twilio.com/docs/sms/tutorials/how-to-receive-and-reply-csharp

The only difference is that I am using .NET MVC Core and not the .NET MVC Framework as provided in the sample. The code I'm having an issue with is:

    [HttpPost]
    public TwiMLResult Index(SmsRequest incomingrequest)
    {
        var messagingResponse = new MessagingResponse();
        messagingResponse.Message("The Robots are coming! Head for the Hills!");
        return TwiML(messagingResponse);
    }

If I encapsulate the method inside a the Controller's Class which by default inherits from the ControllerBase class; the TwiML is identified as a base class and not a method which causes an error.

public class SmsResponseController : ControllerBase
{
    [HttpPost]
    public TwiMLResult Index(SmsRequest incomingrequest)
    {
        var messagingResponse = new MessagingResponse();
        messagingResponse.Message("The Robots are coming! Head for the Hills!");
        return TwiML(messagingResponse); -- <-- Error: Non-invokable member TwiML cannot be used like a method
    }

}

So explicitly following the example, changing the inheritance from the ControllerBase to the TwilioController will solve this specific problem and TwilML is recognized as method with 3 potential overloads.

public class SmsResponseController : TwilioController
{
    [HttpPost]
    public TwiMLResult Index(SmsRequest incomingrequest)
    {
        var messagingResponse = new MessagingResponse();
        messagingResponse.Message("The Robots are coming! Head for the Hills!");
        return TwiML(messagingResponse); -- Method recognized
    }
}

But attempting to execute the program will now throw error during main execution in the Program.cs The error is

System.TypeLoadException: 'Could not load type 'System.Web.HttpContextBase' from assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.'

And unfortunately non of the posts I've found so far seem to be applicable to this scenario nor do they solve the problem.

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}

I can only guess that some portion of code somewhere within the Core Framework is reliant on the existence of the ControllerBase class.

But how can I get the sample to execute without error and dependency on the ControllerBase?

Seems a catch 22 scenario right now.

like image 923
Mark Avatar asked Oct 14 '25 08:10

Mark


1 Answers

There is another package you can use for ASP.NET Core: https://www.nuget.org/packages/Twilio.AspNet.Core/

So, the code you want would be something like this:

using Twilio.AspNet.Common;
using Twilio.AspNet.Core;
using Twilio.TwiML;

namespace TwilioReceive.Controllers
{
    public class SmsController : TwilioController
    {
        public TwiMLResult Index(SmsRequest incomingMessage)
        {
            var messagingResponse = new MessagingResponse();
            messagingResponse.Message("The copy cat says: " +
                                      incomingMessage.Body);

            return TwiML(messagingResponse);
        }
    }
}

For more docs on using .NET Core, you can check out this document: https://www.twilio.com/docs/sms/quickstart/csharp-dotnet-core

like image 112
dprothero Avatar answered Oct 16 '25 22:10

dprothero