简体   繁体   English

Url.Action 返回 null

[英]Url.Action returns null

I am trying to build a email confrimation link using asp.net core 3. After generating the token, generating a confirmEmail link with this code always 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
                );

This is a .net core 3 web api project no mvc.这是一个 .net core 3 web api 项目,没有 mvc。 In start up the configuration is to use: app.UseRouting().在启动时配置是使用:app.UseRouting()。

What could I be doing wrong in the above and why does it always return null.我在上面做错了什么,为什么它总是返回 null。 What do I need to satisfy the method in order for it to return the correct expected value?我需要什么来满足该方法才能返回正确的预期值?

trying to build a email confrimation link using asp.net core 3. After generating the token, generating a confirmEmail link with this code always returns null.尝试使用asp.net core 3 构建电子邮件确认链接。生成令牌后,使用此代码生成confirmEmail 链接始终返回null。

In this documentation , you could find:本文档中,您可以找到:

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.

To answer my question based off the feedback and answers given was able to dig around a bit more on what I was missing.根据给出的反馈和答案来回答我的问题,可以深入了解我遗漏的内容。 The implementation below solved my issue and I was able to generate an email confirmation link.下面的实现解决了我的问题,我能够生成电子邮件确认链接。

Below is the implementation:下面是实现:

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

Passed the "ConfirmEmail" method the nameof() operator as well as setting the user email.将“ConfirmEmail”方法传递给 nameof() 运算符并设置用户电子邮件。 Not sure what goes on in the background with that whole implementation but will save that for another day.不确定整个实现的后台发生了什么,但会将其保存到另一天。

Confirm EMail method:确认电子邮件方法:

[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