Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to an ASP.NET MVC controller when the user navigates away before a response is received?

I have an AJAX action that can take a couple of minutes to complete depending upon the amount of data involved. If a user gets frustrated and navigates away while this action is still running, what happens to the controller? Does it complete anyway? Does it know the request should be abandoned and dispose of the controller object?

like image 308
Raymond Saltrelli Avatar asked Sep 19 '14 13:09

Raymond Saltrelli


People also ask

Is controller responsible for providing the user interface UI to the user?

Controllers are the components that handle user interaction, work with the model, and ultimately select a view to render that displays UI. In an MVC application, the view only displays information; the controller handles and responds to user input and interaction.

Does the controller return a view?

A controller action might return a view. However, a controller action might perform some other type of action such as redirecting you to another controller action.

What does ActionResult return?

An action result is what a controller action returns in response to a browser request. The ASP.NET MVC framework supports several types of action results including: ViewResult - Represents HTML and markup. EmptyResult - Represents no result.

What is the function of the controller in the MVC model?

A controller is responsible for controlling the way that a user interacts with an MVC application. A controller contains the flow control logic for an ASP.NET MVC application. A controller determines what response to send back to a user when a user makes a browser request.


1 Answers

It will not cancel the request to the server as the act of navigating away does not send any information back to the server regarding that request. The client (browser), however, will stop listening for it. After the request finishes, regardless if the client was listening for it or not, the controller will dispose as it typically would.

With that said, you could get fancy and use a combination of listening for a page change on the client side and calling abort on the AJAX request to the server.

This SO question discusses how to abort a request. I could imagine setting a variable when you first start the AJAX request and then unsetting it when it finished.

Warning - Pseudo code below

var isProcessing = false;

var xhr = $.ajax({
    type: "POST",
    url: "myUrl",
    beforeSend: function(){
       isProcessing = true;
    }
    complete: function(){
       isProcessing = false;
    }
});

window.onbeforeunload = function(){
   if(isProcessing){
       xhr.abort();
   }
}

The above is very basic idea of the concept, but there should probably be some checks around if the xhr object exists, perhaps also bind/unbind the window.onbeforeunload in the beforeSend and complete object handlers for the .ajax() item.

like image 102
Tommy Avatar answered Oct 18 '22 20:10

Tommy