RedirectToAction传递到当前操作的结束并且无处可去

时间:2017-04-20 14:20:10

标签: routes twilio castle-windsor asp.net-mvc-5.2

以下是成功点击的控制器路线:

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

RedirectToAction(“DiaryQuestions”)和RedirectToAction(“UnknownCode”)都没有成功重定向。相反,执行移动到当前BeginSmsMessaging操作的末尾,然后滑入IoC代码,控制器被释放,程序执行就停止了。

以下是无法访问的DiaryQuestions行动:

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

无法访问的UnknownCode操作:

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

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

        return TwiML(response);
    }

控制台或Windows事件查看器中不会引发任何错误。此外,在RedirectToAction调用之前,一切看起来都很好 - 输入参数和变量都已正确填充。

此刻我不知道会发生什么。 :(

1 个答案:

答案 0 :(得分:1)

Twilio开发者传道者在这里。

我相信安迪的评论可能是正确的。因为您要返回重定向,所以会向Twilio发送301响应。我无法轻易找到关于Twilio是否遵循这些重定向的文档,但我在这种情况下假设它不是。

我将代码从您的两个额外操作diaryQuestionsunknownCode移至原始操作,并从那里直接返回TwiML

类似的东西:

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

让我知道这是否有帮助。