检测ASP.NET MVC中的中止请求

jdk*_*zek 8 asp.net-mvc asynchronous

在ASP.NET MVC中的异步控制器中,是否有任何方法可以判断客户端是否/何时中止请求?

[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();
}
Run Code Online (Sandbox Code Playgroud)

Wil*_*l D 8

怎么样使用

HttpContext.Current.Response.IsClientConnected
Run Code Online (Sandbox Code Playgroud)

从一个非常基本的测试来看,这似乎不适用于中止的ajax请求.MSDN建议应该这样做.