Response. Redirect (), Server. Execute and Server. Transfer, response. redirect

Source: Internet
Author: User

Response. Redirect (), Server. Execute and Server. Transfer, response. redirect
1. Response. Redirect ():

The Response. Redirect method causes the browser to link to a specified URL.

When the Response. Redirect () method is called, it creates a Response, and the Response Header specifies the status generation.
Code 302 (indicating that the target has changed) and the new target URL. The browser receives the response from the server and sends a request to the new URL using the information in the response header.
That is to say,

When the Response. Redirect method is used, the redirection operation occurs on the client, involving two communications with the server (two back and forth) in total: the first is for the original page

Request, get a 302 response, the second is the new page stated in the request 302 response, get the page after redirection.

 

2. Server. Transfer

Server. transfer is a new feature in IIS 5.0. It solves
Two important defects of Response. Redirect:
1) In Response. Redirect, we cannot get any output on the first page.
2) Response. Redirect will lose all the attributes in the request. Of course, we can do some other
Method, such as session. However, some page parameters are passed in the request. In this case, it won't work.
3) Response. Redirect requires the client to initiate another request.
Server. transfer solves these problems. It initiates a request directly from the server to the next page and does not require the client to send the request again.
If your webpage is very dependent on response. redirect, this small change can increase the efficiency by nearly 25%. (According to Microsoft documentation ).

The Server. Transfer Method transfers the execution process from the current ASPX file to another ASPX page on the same Server. When Server. Transfer is called, the current ASPX page is terminated.

Execute. the execution process is transferred to another ASPX page, but the new ASPX page still uses the response stream created on the previous ASPX page.
If you use the Server. Transfer Method to navigate between pages, the URL in the browser will not change, hiding the address of the new web page and the parameter value attached to the address. Data Confidentiality. The amount of data transferred (the parameters attached to the URL), because the redirection is performed on the server, the browser does not know that the server has executed a page change.
By default, the Server. Transfer method does not pass form data or query strings from one page to another, but you only need to set the second parameter of this method to True.

The form data and query string of the first page can be retained.
At the same time, when using Server. Transfer, note that the target page will use the response stream created on the original page, which leads to ASP. NET Machine verification check (Machine

Authentication Check, MAC). The ViewState of the new page has been tampered. Therefore, to retain the form data and query string set of the original page, you must set the target page

Set the EnableViewStateMac attribute of the Page command to False.

Cause analysis:

Server. one disadvantage of Transfer () is that when the user is in. aspx submits a form, and then uses Server. transfer () enters B. aspx. If you refresh the page,

The browser will ask the user whether to "retry" to send the form. If the user clicks "yes", the data in the form will be re-sent to the server. For example, the function of sending a form is

Insert a record into the database, and the result is not expected to happen-the same form is added to the database multiple times.

Method 1:

Context. items. add ("strFlowUsers", strRet); Context. items. add ("strAttXml", strAttXml); Context. items. add ("strOpinion", strOpinion); Context. items. add ("strFormXMLValue", strFormXMLValue); Server. transfer (".. /Forms/flow_Sender.aspx? MessageID = "+ lngMessageID. toString () + "& ActionID =" + strActionID, true); strUsersXml = Context. items ["strFlowUsers"]. toString (); strValues = Context. items ["strFormXMLValue"]. toString (); strAttXml = Context. items ["strAttXml"]. toString (); strOpinion = Context. items ["strOpinion"]. toString ();View Code

Method 2:

1. Create a web form 2. Place a button1 in the newly created web form, and place two TextBox1 and TextBox2. 3. The code for creating a click event for the button is as follows: private void button#click (object sender, System. eventArgs e) {Server. transfer ("webform2.aspx");} 4. The TextBox1 is returned during the creation process. The value code of the TextBox2 control is as follows: public string Name {get {return TextBox1.Text ;}} public string EMail {get {return TextBox2.Text;} 5. Create a new target page named webform2 6. place two Label1 in webform2. Label2 adds the following code to Page_Load of webform2: private void Page_Load (object sender, System. eventArgs e) {// create the instance WebForm1 wf1 of the original form; // obtain the instantiated handle wf1 = (WebForm1) Context. handler; Label1.Text = wf1.Name; Label2.Text = wf1.EMail ;}View Code

Method 3 (directly obtain the value of the form ):

WebForm1.aspx has a text box named TextBox1. You can use preserveForm to pass it to WebForm2.aspx with True. You can still use

The value of the text box on the Request. Form ("TextBox1") WebForm1.aspx page.

3. Server. Execute:

The Server. Execute method allows the current ASPX page to Execute a specified ASPX page on the same Web Server. When the specified ASPX page is executed, the control process returns to the original page again.

The location where Server. Execute is called.
This page navigation method is similar to calling the callback function for the ASPX page. The called page can access the form data and query string set of the calling page.

Set the EnableViewStateMac attribute of the Page command to False.

 

Differences between Server. Execute and Server. Transfer

Server. Execute ("another. aspx") and Server. Transfer ("another. aspx") are different:
Execute transfers the execution from the current page to the specified page and returns the execution to the current page.
Transfer completely transfers the execution to the specified page
Summary:
When the network status is good, the Redirect (url) method is the most efficient !! Can be redirected to aspx or non-aspx (html) resources on the same or non-Same server
The Server. Transfer Method and Server. Execute method are the most flexible !! But it can only be transferred to the same Application directory, which may lead to unexpected results.
The Server. Execute method occupies the most resources.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.