If your application is hosted by IIS with in-process hosting, a user will be able to cancel the process at the server side according to request from the user. The request means clicking X button on the browser, depressing Esc key on keyboard or executing XMLHttpRequest.abort() method for ajax request.
Below is sample:
Controller / Action Method
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using MvcCore5App4.Models;
using System.Diagnostics;
using Microsoft.AspNetCore.Authorization;
using System.Threading;
using System.Threading.Tasks;
using System;
using Microsoft.AspNetCore.Identity;
using MvcCore5App4.Data;
namespace MvcCore5App4.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public async Task<IActionResult> Cancel(CancellationToken token)
{
_logger.LogInformation($"start: {DateTime.Now:ss.fff}");
await Task.Delay(5000, token);
_logger.LogInformation($"end: {DateTime.Now:ss.fff}");
return View();
}
}
}
View
@{
ViewData["Title"] = "Cancel";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h1>Cancel</h1>
<input type="button" id="ajaxRequest" value="Ajax Request" />
<br />
<input type="button" id="abortButton" value="Abort" />
<br />
<div id="result"></div>
@section Scripts {
<script type="text/javascript">
//<![CDATA[
var xhr;
$(function () {
$('#ajaxRequest').on('click', function (e) {
xhr = $.ajax({
url: '/home/cancel',
method: 'get',
}).done(function (response) {
$("#result").empty;
$("#result").text(response);
}).fail(function (jqXHR, textStatus, errorThrown) {
$("#result").empty;
$("#result").text('textStatus: ' + textStatus +
', errorThrown: ' + errorThrown);
});
});
$('#abortButton').on('click', function (e) {
if (xhr) {
xhr.abort();
}
});
});
//]]>
</script>
}
For detail please read the following article:
Handling aborted requests in ASP.NET Core