How can I abort an action in ASP.NET MVC

I want to stop the actions called by the jQuery.ajax method on the server side. I can stop the Ajax request using the $.ajax.abort() method on the client side, but not on the server side.

Updated:

I used an asynchronous action instead of a sync action, but I did not get what I want! Since you know that the server cannot process more than one request at a time, which makes each request wait until the previous one is completed, even if the previous request is canceled by the $ .Ajax.Abort () method. I know that if I use [SessionState (System.Web.SessionState.SessionStateBehavior.ReadOnly)], then this is almost what I want, but it does not satisfy me.

First of all, I want to abandon the server-side processing method by the user. What is it:)

+6
source share
4 answers

You can look at using the following type of controller. Using an asynchronous controller in ASP.NET MVC

and also find out if this article can also help you Cancel asynchronous calls to web services , sorry that this time I could not provide code examples.

I created an example as a proof of concept to show that you can cancel server-side requests. My gynub async cancellation example

If you call other sites through your code, you have two options, depending on the target structure and which method you want to use. I have included links here for your review:

WebRequest.BeginGetResponse for use in .Net 4.0 HttpClient for use in .Net 4.5, this class has a way to cancel all pending requests.

Hope this gives you enough information to achieve your goal.

+4
source

Here is an example of a backend:

 [HttpGet] public List<SomeEntity> Get(){ var gotResult = false; var result = new List<SomeEntity>(); var tokenSource2 = new CancellationTokenSource(); CancellationToken ct = tokenSource2.Token; Task.Factory.StartNew(() => { // Do something with cancelation token to break current operation result = SomeWhere.GetSomethingReallySlow(); gotResult = true; }, ct); while (!gotResult) { // When you call abort Response.IsClientConnected will = false if (!Response.IsClientConnected) { tokenSource2.Cancel(); return result; } Thread.Sleep(100); } return result; } 

JavaScript:

 var promise = $.post("/Somewhere") setTimeout(function(){promise.abort()}, 1000) 

I hope I'm not late.

+3
source

We saw a problem in IE, where the interrupted request was still redirected to the controller action, however with arguments that were deprived, which led to a different error, it was reported in our logs and user activity records.

I solved this using a filter similar to the following

 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using TWV.Infrastructure; using TWV.Models; namespace TWV.Controllers { public class TerminateOnAbortAttribute : FilterAttribute, IActionFilter { public void OnActionExecuting(ActionExecutingContext filterContext) { // IE does not always terminate when ajax request has been aborted - however, the input stream gets wiped // The content length - stays the same, and thus we can determine if the request has been aborted long contentLength = filterContext.HttpContext.Request.ContentLength; long inputLength = filterContext.HttpContext.Request.InputStream.Length; bool isAborted = contentLength > 0 && inputLength == 0; if (isAborted) { filterContext.Result = new EmptyResult(); } } public void OnActionExecuted(ActionExecutedContext filterContext) { // Do nothing } } } 
0
source

A simple solution is to use something like below. I use it to cancel long-running tasks (in particular, to create thousands of notifications). I also use the same approach to polling progress and updating progress bar through AJAX. In addition, it works on almost any version of MVC and is independent of the new .NET features.

 public class MyController : Controller { private static m_CancelAction = false; public string CancelAction() { m_CancelAction = true; return "ok"; } public string LongRunningAction() { while(...) { Dosomething (ie Send email, notification, write to file, etc) if(m_CancelAction) { m_CancelAction = false; break; return "aborted"; } } return "ok"; } } 
-1
source

Source: https://habr.com/ru/post/949869/


All Articles