How to Abort Long running Ajax Request in .net MVC

2019-08-02 06:43发布

问题:

I've found several answers including this one and also this one. But for some reason this.Response.IsClientConnected still goes as true.

Here is my Client:

<div class="ajax-loader-gif-close" id="cancelBusy">Close</div>

@using (Html.BeginForm("List", "Visit", FormMethod.Post, new { @id = "formFilter" }))
    {
       //Some models some TextBoxFor  etc.
    }

<script type="text/javascript" language="javascript">
    var ajaxCallObject = null;

    $(document).ready(function () {
         $('form').submit(function () {
                ajaxCallObject = $.ajax({
                    url: this.action,
                    type: this.method,
                    data: $(this).serialize(),
                    beforeSend: function () {
                        toggleBusyIndicator();
                    },
                    complete: function () {
                        toggleBusyIndicator();
                    },
                    success: function (result) {
                        //Some stuff
                    }
                });

                return false;
            });
        }
        $("#cancelBusy").click(function () {
             ajaxCallObject.abort();
        });
    });
</script>

Here is my Server-Side:

[HttpPost]
public PartialViewResult List(VisitFilterViewModel model)
{
      //IsClientConnected is ALWAYS TRUE even though I abort from client
      while (this.Response.IsClientConnected)
      {
            Thread.Sleep(500);
      }

      return PartialView("PartialVisitFilterList", model);
}

With FireBug I see that the action is Aborted, but IsClientConnected is still true. What am I doing wrong?

Note: If you consider removing [HttpPost] attribute to solve this, I've already tried and didn't work.

回答1:

Thanks to this post I finally solved it! Actually, what I did was completely working but since I was running this in my local computer with VS Development, the Response was never ended. But, when I put it on IIS Server, everything worked out just fine.

In case site or the post is deleted, I'm posting the answer here:

The Cancel button is working fine when the application is hosted on IIS. If you are testing on the VS development server the Response.IsClientConnected will always be true. Keep in mind that in the previous versions of the RadProgressArea the cancellation is not working under Chrome. For Q2.2014 this will be resolved. You could test our internal build for this.