In an async controller in ASP.NET MVC, is there any way to tell if/when the request is aborted by the client?
[NoAsyncTimeout]
public void IndexAsync() {
var source = new CancellationTokenSource();
Task.Factory.StartNew(() => {
while (true) {
if (source.Token.IsCancellationRequested) {
AsyncManager.Finish();
return;
}
Response.Write(".");
Thread.Sleep(1000);
}
}, source.Token);
// Is there any way to do this?
Request.Aborted += (sender, e) => source.Cancel();
AsyncManager.OutstandingOperations.Increment();
}
What about using
HttpContext.Current.Response.IsClientConnected
From a very basic test this doesn't seem to work for aborted ajax requests. MSDN suggests that it should though.
Try
CancellationToken clientDisconnectedToken = HttpContext.Response.ClientDisconnectedToken;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With