In ASP.Net, how do I run code after the page is submitted

In ASP.Net, I want to run some code (logging, another cleanup) after the page has already been sent to the user. I do not want this code to interfere with the amount of time it takes the client to receive a response. I tried to place this code in the OnUnload part of the page, but through testing (using breakpoints or busy wait cycles), the client does not actually display the page until the code in OnUnload is complete. Despite the fact that the response object is no longer available at the moment, and therefore I would suggest that a buffer response was sent to the client, the client still does not display the page until OnUnload completes. The only way that seems to be working at the moment is to start a new thread to do the work, and enable OnUnload immediately. However i don't knowis it safe or not. Will the server kill the stream if it works too long after the page has already been sent? Is there a better way to do this?

+3
source share
7 answers

Kibby

Try overriding the Page.Render method and reset the answer like this:

Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
    MyBase.Render(writer)

    Response.Flush() ' Sends all buffered output to the client

    ' Run some code after user gets their content
End Sub

I think that at this point the answer is still not complete, but the user will get what the page displayed before completing this final code.

NTN

Mike

+2
source

Unload is the last part of the life cycle of an ASP.NET page. See the link below:

http://msdn.microsoft.com/en-us/library/ms178472.aspx

Is this something you could do with javascript or AJAX?

+1
source

. Unload?

0

, , ISAPI , .

0

- ASP.NET

Windows MSMQ . asp.net front end , Windows .

azure, asp.net -, Windows - !

0

. aspx, "" -. "cleanup.aspx", . Page_Load cleanup.aspx:

public partial class cleanup: System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e) 
    {
        // do logging blah blah here....
    }
}

aspx JavaScript, AJAX "cleanup.aspx". AJAX . jquery, :

$(function(){
    yourAJAXYFunctionName();
});

function yourAJAXYFunctionName()() {
 // ajax code here
 //  
 $.ajax({
   url: "cleanup.aspx",
  });
}

, . , ASPX AJAX, cleanup.aspx, , . / .

: JavaScript. , , JS? , cleanup.aspx , , cleanup.aspx, , , AJAX. AJAX script, .

0

, {Response.End()} catch {} runcode();

-1

Source: https://habr.com/ru/post/1761574/


All Articles