如何在 Core 2.1 中将重定向附加到语音响应 ASP.NET?



Using Twilio 5.16 Asp.Net 核心 2.1.1

我有一个代码示例,我正在尝试从 mvc 移植到 asp.net核心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);
}

}
}

蒂亚

我最终使用了DI内置的aspnet Core

所以在启动配置服务:

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

然后在控制器 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);
}

最新更新