简体   繁体   中英

How to point ASP.NET MVC Url.Action to Angular route without hardcoding?

My Angular (ng app) frontend runs on localhost:4200
My MVC backend runs on localhost:5000

I'm configuring e-mail confirmation.
The e-mail arrives, but now I want the link in that e-mail to point to my frontend so that I can handle the confirmation accordingly.
At the moment, the link in the e-mail (callbackUrl) is null, which is because I'm pointing to a route that doesn't exist in the code (which also makes the link unclickable).

private async Task SendEmailConfirmation(User user, CreateUserModel model)
{
  string token = await _userManager.GenerateEmailConfirmationTokenAsync(user);
  var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = token }, protocol: HttpContext.Request.Scheme);

  await _emailSender.SendEmailAsync(model.Email, "Confirm your account", "Please confirm your e-mail by clicking this link: <a href=\"" + callbackUrl + "\">click here</a>");
}

Is there a way to make callbackUrl point to localhost:4200/userId/EmailConfirmation for example? I just hardcoded the link now, but I'm not sure if this is considered a bad practice?

(hardcoded link: )

  var callbackUrl = "http://localhost:4200/";

I can post more code if necessary.

A more user friendly was is to send the activation code to the user email and ask him to enter it in the same registration form, this will remove the need to have a separate page just for confirmation and it will be the same as if the user is confirming his mobile #.

If you must do it the way you are currently doing then you can have an MVC view just to handle this page and it will be totally seperarte from the Angular App. And in your email, you will put the link to the MVC Action that will check if the user provided the correct activation code or not. Once the user sees the page, provide a link to the Angular App on localhost:4200 where the SPA will be loaded

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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