1
votes

I am trying to receive a request from Twilio through my .NET Core Web API project. I don't want to use MVC. I have followed the tutorial in the Twilio docs here on receiving a message and returning a response but, it seems to only work with an MVC project. I have followed the directions line by line but still "no joy".

When I try and hit the endpoint through Postman I receive a 400 Bad Request response. The body says: "": [ "The input was not valid." ]

Twilio logs the error as Error - 11200 HTTP retrieval failure

Both MVC and Web API controllers "if i'm not mistaken" inherit from the same base class in .Net Core. Does anyone know why this doesn't work with a Web API controller?

1

1 Answers

1
votes

You seem to be confused about things here. There's really no such things as MVC or Web API when it comes to ASP.NET Core. A controller is a controller is a controller. That said, since ASP.NET Core 2.1, "API" controllers generally now inherit from ControllerBase and have an ApiController attribute applied, while "MVC" controllers inherit from Controller. However, that's only the case because if a controller is being used specifically for API stuff, it doesn't necessarily need all the bells and whistles of Controller and the ApiController attribute adds some helpful features specific to APIs. You could just as well have an "API" controller that inherited from Controller without the ApiController attribute, or you could even have an "MVC" controller inherit from ControllerBase.

Long and short, the terms are mere semantics. Calling a controller an "API controller" is shorthand for saying that it's a controller that conforms to REST specifications and has actions that return something like JSON or XML, whereas calling a controller an "MVC controller" is just shorthand for saying that it's a controller whose actions return Razor views.

That said, the Twilio docs actually have you using a TwilioController, which while I haven't dug into the source is likely just derived from ControllerBase and implements the webhook handling boilerplate for you. As such, if your question is why you cannot use just plain Controller or ControllerBase here instead of TwilioController, the answer is likely that you can if you implement the same code that it does yourself.