This is an update for 2023.
You can easily utilize the DELETE verb of an Ajax request:
public async Task<IActionResult> OnDeleteAsync(int id)
{
await _myService.DeleteUser(id);
return new OkResult();
}
With jQuery it looks something like this:
<script>
$(".deletebtn").on("click", function (e) {
deleteUser($(this).data("id"));
});
const deleteUser = function (userId) {
$.ajax({
method: 'DELETE', // specify HTTP verb.
url: '/account/users', // specify right path to your controller/razor page
headers:
{
"RequestVerificationToken": "<get your token here>"
},
data: { id: userId },
success: function () {
window.location.href = "/account/users"; // reload page
},
error: function (response) {
alert(response);
}
});
};
</script>
<button class="deletebtn" data-id="123">Delete</button>
<button class="deletebtn" data-id="456">Delete</button>
<button class="deletebtn" data-id="1111">Delete</button>
Similarly the HTTP verbs GET, POST, PUT can also be utilized:
OnGet(), OnPost(), OnPut()
and their async equivalents
OnGetAsync(), OnPostAsync(), OnPutAsync()
Usage e.g.:
OnGetUsersAsync() // GET a list of Users async.
OnPostUserAsync() // POST (add) a User async.
OnPutUpdateMyUserAsync() // PUT (update) a user async
OnDeleteCoffeCup(id) /// DELETE method sync.
In addition you can add your own experimental HTTP verbs. See e.g. here https://khalidabuhakmeh.com/adding-experimental-http-methods-to-aspnet-core