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.