如何在ASP.NET Core 2.1中将重定向附加到VoiceResponse?

时间:2018-08-23 00:33:00

标签: twilio

使用Twilio 5.16 Asp.Net Core 2.1.1

我有一个代码示例,我试图从asp.net mvc移植到asp.net 核心 我在确定该行使用什么时遇到问题:

response.Redirect(Url.ActionUri("ShortWelcome", "IVR"));

因为Url上不再存在“ ActionUri”方法。

我的控制器操作:

using Twilio.AspNet.Core;
using Twilio.TwiML;
using Twilio.TwiML.Voice;

namespace IVRPhoneTree.Core.Web.Controllers
{
    public abstract class ControllerBase : TwilioController
    {
        public TwiMLResult RedirectWelcome()
        {
            var response = new VoiceResponse();
            response.Say("Returning to the main menu ", Say.VoiceEnum.PollyBrian, 1, Say.LanguageEnum.EnAu);
            response.Redirect(Url.ActionUri("Welcome", "IVR"));

            return TwiML(response);
        }



        public TwiMLResult RedirectBadPin()
        {
            var response = new VoiceResponse();
            response.Say("Sorry that pin is not correct. Returning you to the main menu. ",
                Say.VoiceEnum.PollyBrian, 1, Say.LanguageEnum.EnAu);
            response.Redirect(Url.ActionUri("ShortWelcome", "IVR"));

            return TwiML(response);
        }


    }
}

TIA

1 个答案:

答案 0 :(得分:0)

我最终使用了DI中内置的aspnet核心

因此在启动ConfigureServices中:

services
            .AddSingleton<IActionContextAccessor, ActionContextAccessor>()
            .AddScoped<IUrlHelper>(x => x
                .GetRequiredService<IUrlHelperFactory>()
                .GetUrlHelper(x.GetRequiredService<IActionContextAccessor>().ActionContext));

然后在Controller ctr中输入

 private readonly IUrlHelper _urlHelper;
  public IVRController(IUrlHelper urlHelper)
  {
      _urlHelper = urlHelper;
  }

这使我能够:

 public TwiMLResult RedirectBadPin()
    {
        var response = new VoiceResponse();
        response.Say("Sorry that pin is not correct. Returning you to the main menu. ",
            Say.VoiceEnum.PollyBrian, 1, Say.LanguageEnum.EnAu);

        string path = _urlHelper.Action("ShortWelcome", "IVR");

        response.Redirect(new Uri(path, UriKind.Absolute), HttpMethod.Get);   

        return TwiML(response);
    }