routestwiliocastle-windsorasp.net-mvc-5.2

RedirectToAction passes to end of current action and goes nowhere


Here is the controller route that is being hit successfully:

[Route("begin")]
    public ActionResult BeginSmsMessaging(SmsRequest message)
    {            
        string from = message.From;

        var phoneNumber = from.Replace("+1", "").FormatPhoneNumber();            

        _commandPipeline.Publish(new LogInboundMessage
        {
            PhoneNumber = phoneNumber,
            TimestampUtc = DateTime.UtcNow
        });

        int code;
        if(int.TryParse(message.Body, out code)) 
        {
            try
            {
                return RedirectToAction("DiaryQuestions");
            }
            catch(Exception e)
            {
                string error = e.Message;
                return null;
            }                
        }
        else
        {
            return RedirectToAction("UnknownCode");
        }
    }

Neither the RedirectToAction("DiaryQuestions") nor the RedirectToAction("UnknownCode") are successfully redirecting. Instead, execution moves to the end of the current BeginSmsMessaging action and then slips into IoC code where the controller is released and program execution just stops.

Here is the unreachable DiaryQuestions action:

[Route("diaryQuestions")]
    public ActionResult DiaryQuestions(SmsRequest message)
    {
        var response = new TwilioResponse();
        response.SetAttributeValue("PhoneNumber", message.From);
        response.SetAttributeValue("DiaryQuestion", "1");
        response.Message("This is a test message.");
        response.Sms("This is a test SMS");
        return TwiML(response);
    }

And the unreachable UnknownCode action:

[Route("unknownCode")]
    public ActionResult UnknownCode(SmsRequest message)
    {
        var response = new TwilioResponse();

        response.Sms("What to say...");

        return TwiML(response);
    }

No errors are thrown in either the console or Windows Event Viewer. Also, everything looks good up until the RedirectToAction call - input parameters and variables are all populated correctly.

I have no idea at this point what could be happening. :(


Solution

  • Twilio developer evangelist here.

    I believe Andy is probably right in his comments. Because you are returning a redirect, this will send a 301 response to Twilio. I can't easily find documentation on whether Twilio follows those redirects, but I'd assume in this case that it is not.

    I'd move the code from your two extra actions, diaryQuestions and unknownCode, to the original action and return the TwiML straight from there.

    Something like:

    [Route("begin")]
        public ActionResult BeginSmsMessaging(SmsRequest message)
        {            
            string from = message.From;
    
            var phoneNumber = from.Replace("+1", "").FormatPhoneNumber();            
    
            _commandPipeline.Publish(new LogInboundMessage
            {
                PhoneNumber = phoneNumber,
                TimestampUtc = DateTime.UtcNow
            });
    
            var response = new TwilioResponse();
    
            int code;
            if(int.TryParse(message.Body, out code)) 
            {
                try
                {
                    response.SetAttributeValue("PhoneNumber", message.From);
                    response.SetAttributeValue("DiaryQuestion", "1");
                    response.Message("This is a test message.");
                    response.Sms("This is a test SMS");
                }
                catch(Exception e)
                {
                    string error = e.Message;
                    return null;
                }                
            }
            else
            {
                response.Sms("What to say...");
            }
    
            return TwiML(response);
        }