J
JDias5
Guest
I am creating an app in C# and ASP.NET Core MVC, in which there is a View with a submit button like this:
<form method="POST" action="/Controller/Action">
<!-- ... -->
<input type="submit" value="Click here!" />
<!-- ... -->
</form>
After pressing in the submit button, the action /Controller/Action is triggered, but may take a while to execute, and then it is possible to click in the submit button several times while the action is executing... This is very undesirable, once the same data in inserted in the database several times!
I know that the safest way to do this is in the server-side, since everything in the client-side may be forged...
The code that I have for this action is:
[HttpPost]
public async Task<IActionResult> Action(MyModel model)
{
UserModel user = HttpContext.Session.GetJson<UserModel>("User");
if (user.AwaitingSubmit)
// what to put here!!!!
;
user.AwaitingSubmit = true;
HttpContext.Session.SetJson("User", user); // --------
// ... long and time-expensive code ...
// ... writes in the database ...
user.AwaitingSubmit = false;
HttpContext.Session.SetJson("User", user);
return RedirectToAction("Action2");
}
But this code doesn't prevent for the multiple insertion in the database... The first thread doesn't reach the line marked // -----
so the control flag (AwaitingSubmit) isn't updated! (I also have a problem to cancel the undesired tasks... gives an error)
Since I potentially may have several users running this routine at the same time, what I may do to prevent this routine to be triggered more than once for the same user, for the same user request..., at the same time...
Should I do something in the middleware level? Does this have a solution? Or must I do this in the client-side?
Continue reading...
<form method="POST" action="/Controller/Action">
<!-- ... -->
<input type="submit" value="Click here!" />
<!-- ... -->
</form>
After pressing in the submit button, the action /Controller/Action is triggered, but may take a while to execute, and then it is possible to click in the submit button several times while the action is executing... This is very undesirable, once the same data in inserted in the database several times!
I know that the safest way to do this is in the server-side, since everything in the client-side may be forged...
The code that I have for this action is:
[HttpPost]
public async Task<IActionResult> Action(MyModel model)
{
UserModel user = HttpContext.Session.GetJson<UserModel>("User");
if (user.AwaitingSubmit)
// what to put here!!!!
;
user.AwaitingSubmit = true;
HttpContext.Session.SetJson("User", user); // --------
// ... long and time-expensive code ...
// ... writes in the database ...
user.AwaitingSubmit = false;
HttpContext.Session.SetJson("User", user);
return RedirectToAction("Action2");
}
But this code doesn't prevent for the multiple insertion in the database... The first thread doesn't reach the line marked // -----
so the control flag (AwaitingSubmit) isn't updated! (I also have a problem to cancel the undesired tasks... gives an error)
Since I potentially may have several users running this routine at the same time, what I may do to prevent this routine to be triggered more than once for the same user, for the same user request..., at the same time...
Should I do something in the middleware level? Does this have a solution? Or must I do this in the client-side?
Continue reading...