title: Abort AJAX request via CancellationToken in ASP.NET MVC
body: <p>I have an ASP.NET MVC application that interacts with external resources and there is an operation which takes a lot of time.
  So in controller I have method like this</p>
  
  <pre><code>[HttpPost]
  public async Task&lt;JsonResult&gt; SomeMethod(..., CancellationToken token)
  {
      await _someService.ExecuteSlowOperationAsync(..., token);
      ...
  }
  </code></pre>
  
  <p>And this slow operation looks like</p>
  
  <pre><code>public async Task ExecuteSlowOperationAsync(..., CancellationToken token)
  {
      return await Task.Run(() =&gt; 
      {
         //interacting with external resource
      }, token);
  } 
  </code></pre>
  
  <p>This method linked with modal view and if request will take so much time user might decide to close it. According to that I have to cancel request without awaiting results, so on client-side I have code like</p>
  
  <pre><code>...
  var request = $.ajax(...);
  
  ...
  
  $('#modal').on('hidden.bs.modal', function () {
    request.abort();
  });
  </code></pre>
  
  <p>If I understood this <a href="https://www.davepaquette.com/archive/2015/07/19/cancelling-long-running-queries-in-asp-net-mvc-and-web-api.aspx" rel="noreferrer">article</a> correctly, cancellation token binds with request via framework model binder and no need to do something with it. When user close modal form, in browser concole I can see that request gets status "canceled" but on server-side slow operation is still executing. Aslo I tried with</p>
  
  <pre><code>CancellationToken disconnectedToken = Response.ClientDisconnectedToken;            
  var source = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, disconnectedToken);
  </code></pre>
  
  <p>and then took token from this source, but still got nothing.</p>
  
  <p>I feel like I'm missing some important thing and have misunderstanding of this situation. Any ideas how to make it work?</p>