コード例 #1
0
        public async Task <IActionResult> OnPostAsync()
        {
            // Check if the reCaptcha is valid.
            if (!await _reCaptchaChecker.IsValid(Input.ReCaptchaToken))
            {
                // Add an error to the model.
                ModelState.AddModelError(string.Empty, "The reCaptcha verification failed.");
                // Return the page.
                return(Page());
            }
            // Check if the provided model is not valid.
            if (!ModelState.IsValid)
            {
                // Add an error to the model.
                ModelState.AddModelError(string.Empty, "An error was encountered. Please check again the input fields.");
                // Return the page.
                return(Page());
            }
            // Define a new view model for the e-mail.
            var emailViewModel = new EmailContactViewModel
            {
                Name           = Input.Name,
                Email          = Input.Email,
                Message        = Input.Message,
                ApplicationUrl = _linkGenerator.GetUriByPage(HttpContext, "/Index", handler: null, values: null)
            };
            // Send an e-mail to the predefined administrator address.
            await _emailSender.SendContactEmailAsync(emailViewModel);

            // Display a message to the user.
            TempData["StatusMessage"] = "Success: The message was sent successfully.";
            // Redirect to page.
            return(RedirectToPage());
        }