Comparison and selection of four page navigation methods for ASP.NET

xiaoxiao2021-03-06  50

In ASP.NET applications, there are a variety of ways between web forms: with a hyperlink, with Response.Redirect, with Server.Transfer, or with Server.execute. This article will analyze the four navigation methods and its excellent disadvantages to help you choose the best navigation.

First, hyperlink

The easiest way to enter another form from a form is to use the HTML hyperlink control. In the web form, use hyperlink HTML code classes such as:

When the user clicks the hyperlink, WebForm2.aspx executes and sends the result to the browser. Hyper link navigation is almost available anywhere, including HTML pages, and normal ASP pages. ASP.NET also provides another way of replaceable use, namely HyperLink server control:

NavigateURL = / WebForm2.aspx /> Enter Form 2

The operation results of the above HTML code are the same as the first example because ASP.NET treats the HyperLink web server control as an HTML hyperlink control. But both have a little important difference, HyperLink Web Server Controls can be programmed on server-side. Specifically, it can be changed in the program code, allowing constructing a specific target to dynamically change the hyperlink based on the current state of the application, for example:

Private sub button1_click

Byval sender as system.Object, _

ByVal e as system.eventargs)

Handles button1.click

Hyperlink1.navigateURL = /webform3.asp/

End Sub

After this code is executed, if the user clicks on the link, he sees will be Webform3.aspx instead of WebForm2.aspx.

Second, use program control redirection

Although the hyperlink can navigate from a page to another, this navigation method is completely controlled by the user. Sometimes, we may have to use code to control the entire navigation process, including when to go to another page. In these cases, ASP.NET has three different ways to achieve similar purposes: call the REDIRECT method of the Response object, call the Transfer or Execute method of the Server object. These three navigation methods are basically similar, but there is also a difference.

2.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, indicating that the status code 302 (indicating the target has changed) and the new target URL. The browser receives the response from the server and uses the information in the response head to issue a request for the new URL.

That is to say, the redirection operation occurs in the client when using the response.redirect method, involving two communication with the server (two rounds): The first time is a request for the original page, get a 302 answer, second The second is the new page declared in the request 302 to get the page after redirect.

2.2 Server.Transfer

Server.Transfer methods Turn the execution process from the current ASPX file to another ASPX page on the same server. When calling Server.Transfer, the current ASPX page is terminated, the execution process is transferred to another ASPX page, but the new ASPX page still uses the response created by the previous ASPX page. If you use the Server.Transfer method to implement the navigation between the page, the URL in the browser will not change because the redirection is completely on the server side, and the browser does not know that the server has already executed a page transformation at all.

By default, the Server.Transfer method does not pass the form data or query string from one page to another, but as long as the second parameter of the method is set to true, you can retain the form of the first page. And query strings.

At the same time, attention should be noted when using Server.Transfer: The target page will use the response created by the original page, which causes the ASP.NET's machine authentication check (Mac) that the viewState of the new page has been tampered with. Therefore, if you want to keep the form data and query string collection of the original page, you must set the enableViewStateMac property of the target page to false.

2.3 Server.execute

The server.execute method allows the current ASPX page to perform a specified ASPX page on the same web server. When the specified ASPX page is executed, the control process re-returns the location of the server.execute call.

This page navigation mode is similar to a function call to the ASPX page. The page that is called can access the form data and query string collection that sent the call page, so set the enableViewStateMac property of the called page Page instruction to false.

By default, the output called the page is appended to the current response. However, the Server.execute method has an overloaded method that allows the output of the called page to be called through a TextWriter object (or its sub-object, such as StringWriter object), not directly to the output stream, which is in the original page. It can be convenient to adjust the location of the resulting result of the called page.

To illustrate the working process, let's create a web form, put a button control (Button1) and a text control (Literal1), transfer to the code view in the design interface, join a system.io namespace of the Imports statement, then Join the code to execute when you click the button:

Private sub button1_click

Byval sender as system.Object, _

ByVal e as system.eventargs)

Handles button1.click

DIM SW as stringwriter = new stringwriter ()

Server.execute (/webform2.aspx/, sw)

Literal1.text = sw.toString ()

End Sub

Then create a second page WebForm2.aspx for the same web application. Transfer to the HTML view of the page, modify its Page instruction to prohibit the ViewState check:

<% @ Page language = / vb / autoeventwireup = / false / codebehind = / Webform2.aspx.vb /

Inherits = / Navigate.Webform2 / EnableViewStateMac = / false /%> Go to the design view, add some controls for the second page. Next, set the first page to the default page, start the application. Click the button, the WebForm2 control will display the place where the Literal button is placed in WebForm1, pay attention to the page title and URL still display the original page WebForm1.

When you use the server.transfer or server.execute method, you should pay attention to a point: the final page may not be a legitimate HTML page, because the page that is finally returned to the client may contain multiple and and other tags. IE views seem to tolerate and handle this situation correctly, but if users want to use other browsers, it is best to test it carefully.

Three, compare and select

Since there are so many ways to navigate to another page from a page, how should I choose the best navigation method? Here are some factors that need to be considered:

· If you want to let the user decide when to convert the page and go to which one page, the hyperlink is most suitable.

· If you want to control the target to control the target, the time to convert is determined by the user, dynamically set its navigateURL attribute using the HyperLink control of the web server.

· Use the response.redirect if you want to connect the user to another server.

· connects the user to non-ASPX resources with Response.Redirect, such as the HTML page.

· If you want to keep the query string as part of the URL, use response.redirect.

· If you want to transfer the execution process to another ASPX page of the same web server, you should use Server.Transfer instead of Response.Redirect, because Server.Transfer can avoid unnecessary network communication, gain more Good performance and browsing effect.

· If you want to capture an output result of an ASPX page, use Server.execute if you insert the result into another ASPX page.

· If you want to make sure the HTML output is legal, use Response.Redirect, do not use the Server.Transfer or Server.execute method.


New Post(0)