开发者

ASP.Net Response.Close issue

开发者 https://www.devze.com 2023-01-07 03:37 出处:网络
I am using ASP.Net + .Net 3.5 + VSTS 2008 + IIS 7.0 + C# to develop a web application. I find when debugging in VSTS 2008, if I call Response.Close() method in page_load (please refer to code below),

I am using ASP.Net + .Net 3.5 + VSTS 2008 + IIS 7.0 + C# to develop a web application. I find when debugging in VSTS 2008, if I call Response.Close() method in page_load (please refer to code below), there will be error (from IE when accessing the page) like can not connect to server.

My question is,

  1. Normally when should we call Response.Close()? Or no need to call (rely on ASP.Net framework to automatically close)?

BTW: my previous understanding is developer should always call Response.Close when processing is completed at server side and all data has been written to client using Response.Write. Am I correct?

2 Why I met with such error in my code? What is the ro开发者_运维问答ot cause?

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("Hello World! ");
        Response.Close();
    }


The following from the MSDN website might be useful here:

This method terminates the connection to the client in an abrupt manner and is not intended for normal HTTP request processing. The method sends a reset packet to the client, which can cause response data that is buffered on the server, the client, or somewhere in between to be dropped.

You might use this method in response to an attack by a malicious HTTP client. However, typically you should call CompleteRequest instead if you want to jump ahead to the EndRequest event and send a response to the client.


You should not normally use the Response.Close method in "normal" ASP.NET processing.

All of your data that is written to a HttpResponse "stream" is buffered before being sent to the client browser. The Response.Close method will abruptly terminate the HTTP connection and you may lose data that you have previously Response.Written inadvertently.

If you really want to programmatically "force" the end of a response stream, you should use either: Response.Flush(); followed by Response.End();

The Response.Flush method call ensures that all data that you may have written to the response stream is "flushed" to the client, and Response.End ensures all currently buffered data is correctly sent to client, and also raises the EndRequest event, which you may want to handle.

You can also use the HttpApplication's CompleteRequest() method.

The MSDN documentation states it best:

This method terminates the connection to the client in an abrupt manner and is not intended for normal HTTP request processing. The method sends a reset packet to the client, which can cause response data that is buffered on the server, the client, or somewhere in between to be dropped.

You might use this method in response to an attack by a malicious HTTP client. However, typically you should call CompleteRequest() instead if you want to jump ahead to the EndRequest event and send a response to the client.


In my experience there is no reason to call Response.Close(); within the code example you've provided, just remove it.

In the pages lifecycle after the Page_Load is fired, there are a number of other methods that will be called that will close your responses for you.

Read here for the Page Lifecycle


To answer question 1:

You should call Response.Close() when your need to terminate the connection - that is, nothing else needs to be sent to the client at all. This does not include what you have posted, since the rest of the page needs to be processed and sent to the client. Normally you would call it when returning data that is not a page from an aspx page (for example a pdf file, an image etc...).

To answer question 2:

You should not call Response.Close() in your Page_Load event handler - it will mean that the rest of the page lifecycle will not run properly.

From MSDN (HttpResponse.Close method):

This method terminates the connection to the client in an abrupt manner and is not intended for normal HTTP request processing. The method sends a reset packet to the client, which can cause response data that is buffered on the server, the client, or somewhere in between to be dropped.


.NET is a very flexible network it will let you do anything you could before .NET. (and more obviously). But the most wonderfull thing is that .NET will take care of verything it can take care of for you.

That means if you create and empty web page and run it in your browser you don't have to do anything else to make it work.

Sometimes however you might find yourself in a situation where you need to do something extraordinary and you will be thankfull for the existance of functions like Reponse.Close()

In your case you're not doing such a thing so there's no need for any special function calling.

Besides that Response.Write() is what we used to use back in the days...Are you still thinking in the classic ASP mode maybe?

Suggestion: Don't use Response.Write()

But put a label in your web page and use:

this.Label1.Text = "Hello world";

Addtional comment:

The purpose of ASP.Net in particular is to send web pages to a browser, collect any posted data, process it, interact with the server OS and so on.

So you might, in my opinion, assume that some care has been taken in 1) serving pages fast and 2) making sure nothing goes wrong when the user follows the guide lines on how to program .Net web pages.

There's no need to implement ALL Page event handlers. Understand the framework, understand what each page event does and learn when to implement which event.

If you're only going to show data from a database you don't even need event handlers. Read about the Data Controls (Data sources, GridView, ListView, Repeater, etc).

Assume that if you do nothing, the framework will do it for you. (IF you do nothing at all, nothing happens, that's by design)

Cheers.

0

精彩评论

暂无评论...
验证码 换一张
取 消