Cancel Request after press button

Ibrahim Shawky 20 Reputation points
2024-05-06T17:37:48.1266667+00:00

Hello,

We are using MVC .NET Core framework 6 and we need to cancel or kill request after send. For example after press a button that execute some actions in oracle Database. We need to Cancel this request from another button.

Is this possible? and if not what's the best solution?

Thanks

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,572 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,344 questions
ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
314 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 60,361 Reputation points
    2024-05-06T18:43:19.7166667+00:00

    the easiest approach is to have a request id for each page render. a post or ajax call would pass this request id. server side on the request a cancelation token would be added to a static collection keyed by request id. the second request (cancel button) can pass the request id and call cancel on the token (if found).

    this will be more complex if using a webfarm unless you use sticky sessions.

    0 comments No comments

  2. SurferOnWww 2,406 Reputation points
    2024-05-07T01:19:33.3666667+00:00

    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.

    enter image description here

    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

    0 comments No comments