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,905 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,596 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.
341 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 66,061 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 3,031 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

  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  4. Rahul 0 Reputation points
    2024-09-04T00:30:24.0933333+00:00

    ,

    We are using MVC .NET Core framework 6 and we need to cancel or kill request after send. +91 6207187697.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

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.