Ditty
Ditty

Reputation: 529

REST API post using Twilio.MVC is not working. 404 error message with resource not found

What am I doing wrong in this code? When I send a POST request to this API i get the 404 error page. Can somebody help me debug this code. New to Rest Api and Twilio.

using System.Web.Mvc;
using Twilio.TwiML.Mvc;
using Twilio.Mvc;

namespace MVC4Test.Controllers
{
   public class HomeController : TwilioController
   {
      [HttpPost]
      public TwiMLResult ActionResult(SmsRequest request)
      {
        string smsTextType  = request.Body;
        var response = new Twilio.TwiML.TwilioResponse();

        Response.ContentType = "text/xml";    
        response.Redirect("http://www.google.com");

        //Response.ContentEncoding = System.Text.Encoding.UTF8;    
        //Response.Write(smsTest);    
        return TwiML(response);
        //Response.Close();

      }
   }
 }

This is my route

    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 }
        );
    }

Upvotes: 1

Views: 631

Answers (1)

Devin Rader
Devin Rader

Reputation: 10366

Twilio evangelist here.

Can you post the URL your are trying to load?

My first guess is that you mistakenly named your Action method. Right now its this:

public TwiMLResult ActionResult(SmsRequest request)

That would mean the URL you need to request is:

http://[yourdomain]/Home/ActionResult

If you change the name of the method:

public ActionResult Sms(SmsRequest request)

Then your URL would be:

http://[yourdomain]/Home/Sms

A couple of other things I noticed:

  • If you are using the TwiML() helper method to return the response, you do not need to set the ContentType yourself. That method will do it for you.
  • Not sure why you are telling Twilio to redirect to Google. You might want to look at the docs for <Redirect>

Hope that helps.

Upvotes: 1

Related Questions