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

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (109)

我正在使用Twilio 5.16 Asp.Net Core 2.1.1

我有一个代码示例,我试图从asp.net mvc 移植到asp.net core。我在解决该行使用的问题时遇到问题:

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


    }
}
提问于
用户回答回答于

我最终使用内置DI的aspnet core

所以在启动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);
    }

扫码关注云+社区

领取腾讯云代金券