繁体   English   中英

Url.Action 返回 null

[英]Url.Action returns null

我正在尝试使用asp.net core 3 构建电子邮件确认链接。生成令牌后,使用此代码生成confirmEmail 链接始终返回null。

var token = await _userManager.GenerateEmailConfirmationTokenAsync(user);
            var confirmationLink = Url.Action(
                "ConfirmEmail",
                "Users",
                new { userId = user.Id, token }, protocol: Request.Scheme
                );

这是一个 .net core 3 web api 项目,没有 mvc。 在启动时配置是使用:app.UseRouting()。

我在上面做错了什么,为什么它总是返回 null。 我需要什么来满足该方法才能返回正确的预期值?

尝试使用asp.net core 3 构建电子邮件确认链接。生成令牌后,使用此代码生成confirmEmail 链接始终返回null。

本文档中,您可以找到:

Generating a URI for an invalid route (a controller/action or page that doesn't 

exist) will produce an empty string under endpoint routing instead of producing 

an invalid URI.

根据给出的反馈和答案来回答我的问题,可以深入了解我遗漏的内容。 下面的实现解决了我的问题,我能够生成电子邮件确认链接。

下面是实现:

var confirmationLink = Url.Action(nameof(ConfirmEmail), "Users", new { token, email = user.Email }, Request.Scheme);

将“ConfirmEmail”方法传递给 nameof() 运算符并设置用户电子邮件。 不确定整个实现的后台发生了什么,但会将其保存到另一天。

确认电子邮件方法:

[HttpGet]
public async Task<IActionResult> ConfirmEmail(string token, string email)
{
     var user = await _userManager.FindByEmailAsync(email);
     // More logic....

     var result = await _userManager.ConfirmEmailAsync(user, token);
     // More logic.... return whether success or failure
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM