简体   繁体   中英

Routing issue in ASP.NET CORE 3.0

Using .Net Core 3.0.
This is basically as account verification module that sends user account confirmation email.
Below is the code i wrote to create a URL and send to user's email.
Confirmation link to be sent in the email is generated using LinkGenerator library and than concatenated with localhost to create a complete URL.
The URL sent in the email is created as below and SendAccountConfirmationEmail method is used to send email to the user.

[HttpPost("registerUser")]
    public IActionResult NewUser(UserModel userModel)
    {

        EmailSender emailSender = new EmailSender();
        string response = userDAL.NewUser(userModel).ToString();
        if (response != "EmailExists" && response != "UsernameExists")
        {
            EncryptDecrypt _encryptDecrypt = new EncryptDecrypt();
            string code = emailSender.GenerateEmailCode();
             string LinkEmail= _encryptDecrypt.Encrypt(response, "1359Mali_");
            string url = _linkGenerator.GetPathByAction("ConfirmEmail", "User", new { email = LinkEmail, code = code });
            url = "https://localhost:44312" + url;

            bool EmailSent = emailSender.SendAccountConfirmationEmail(response,url);
            if (EmailSent == true)
            {
                userDAL.InsertUserEmailConfirmationCode(response, code);
            }
        }

        return Ok(response);
    }

I successfully recieve the email with redirection link in it but when i click on the link it opens in the browser and shows Page Not Working with Http Error405 and the debugger on the ConfirmEmail method is not hit. ConfirmEmail method is given below :

 [HttpPost("ConfirmEmail/{email}/{code}")]
    public IActionResult ConfirmEmail( string email, string code)
    {
        EncryptDecrypt _encryptDecrypt = new EncryptDecrypt();
        string emailDecrypted = _encryptDecrypt.Decrypt(email, "1359Mali_");
        string codeDecrypted = _encryptDecrypt.Decrypt(code, "1359Mali_");
        string response = Convert.ToString(userDAL.VerifyCode(emailDecrypted, codeDecrypted));

        if (response.ToUpper() == "EMAILNOTCONFIRMED")
        {
            string message = "EMAILNOTCONFIRMED";
            return StatusCode(200, message);
        }
        else if (response.ToUpper() == "EMAILCONFIRMED")
        {
            //Call login medthod
            return Ok();
        }
        else
        {
            string message = "An Error Occured";
            return StatusCode(200, message);
        }

    }  

电子邮件中收到的链接

StartUp.cs

 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseRouting();
        app.UseHttpsRedirection();



        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });

    }

When a browser opens on a page. That is a Get request. So you should change:

[HttpPost("ConfirmEmail/{email}/{code}")]
public IActionResult ConfirmEmail( string email, string code)

to:

[HttpGet("ConfirmEmail/{email}/{code}")]
public IActionResult ConfirmEmail( string email, string code)

Using HttpPost is not possible in email links. Please either update your action to receive GET requests by marking it as HttpGet.

If you want your action to receive HttpPost requests also, then you can add both HttpGet as well as HttpPost, so action will receive both GET and POST requests.

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