Within my controller method, I am performing a database lookup for the logged-in user and then sending a confirmation email. Here is the code snippet:
[HttpPost]
public async void ResendConfirmationEmail(string username)
{
var user = await Usermanager.FindByNameAsync(username);
try { SendRegistrationEmail(user); }
catch (Exception e)
{
//TODO: LOG EMAIL ERROR
throw e;
}
}
I intend to trigger this function using an AJAX request like so:
var ResendRegEmail = function (identity) {
$.ajax({
type: "POST",
url: "/Customer/ResendConfirmationEmail",
data: { username: identity },
success: function (data) {
$("#ResendModal").modal('show');
$("#msgSuccess").html("Email has been sent successfully. Please check your email.");
},
error: function (request, textStatus, errorThrown) {
$("#ResendModal").modal('show');
$("#msgError").html(textStatus + " " + errorThrown);
}
})
}
[AllowAnonymous]
public async void SendRegistrationEmail(JustAskUser user)
{ code here }
If I remove the 'async' keyword, the `resendConfirmationEmail` function works fine. However, it is required to use 'async' to maintain site speed. Currently receiving a resource won't load 500 error.
There is an issue in the `sendRegistrationEmail` function - {"An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async=\"true\" %>. This exception may also indicate an attempt to call an \"async void\" method, which is generally unsupported within ASP.NET request processing. Instead, the asynchronous method should return a Task, and the caller should await it."}