When developing asp.net, redirecting between different pages is a common problem, when a complex logic cannot be divided into two or more pages for processing on a single page, you need to jump between pages. You can use the most logon logic.
ASP. NET uses the most jump is Response. redirect. This command can directly Redirect requests to a relative or absolute path. it will directly redirect the Http stream blocking of the current page to the new URL.
Server. transfer: this command is directly redirected by the IIS server on the server side. The jump PATH must be a relative PATH, that is, the PATH or virtual eye directory under the same website. it cannot be the absolute path of the Internet, because the path of the request on the same site is directly specified by the same HTTP handle of the current request page.
Where are their differences? Let's first look at the HTTP header and first look at Response. Redirect.
Protected void Page_Load (object sender, EventArgs e)
{
Response. Redirect ("/redirect. aspx"); // Redirect directly to the path on this site
}
Http://xml.test.com/HttpStatus.aspx --> This refers to the page where my request is executed
GET/HttpStatus. aspx HTTP/1.1
Host: xml.test.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv: 1.9.0.14) Gecko/2009082707 Firefox/3.0.14
Accept: text/html, application/xhtml + xml, application/xml; q = 0.9, */*; q = 0.8
Accept-Language: zh-cn, zh; q = 0.5
Accept-Encoding: gzip, deflate
Accept-Charset: gb2312, UTF-8; q = 0.7, *; q = 0.7
Keep-Alive: 300
Connection: keep-alive
HTTP/1.x 302 Found --> pay attention to this 302
Cache-Control: private
Content-Type: text/html; charset = UTF-8
Location:/redirect. aspx
Server: Microsoft-Microsoft IIS/7.5
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP. NET
Date: Sun, 01 Nov 2009 12:12:45 GMT
Content-Length: 133
----------------------------------------------------------
Http://xml.test.com/redirect.aspx --> This is the page for redirect requests
GET/redirect. aspx HTTP/1.1
HOST: xml.test.com
User-Agent: Mozilla/5.0 (windows; U; Windows NT 6.1; ZH-CN; RV: 1.9.0.14) Gecko/2009082707 Firefox/3.0.14
Accept: text/html, application/XHTML + XML, application/XML; q = 0.9, */*; q = 0.8
Accept-language: ZH-CN, ZH; q = 0.5
Accept-encoding: gzip, deflate
Accept-charset: gb2312, UTF-8; q = 0.7, *; q = 0.7
Keep-alive: 300
Connection: keep-alive
HTTP/1.x 200 OK
Cache-control: Private
Content-Type: text/html; charset = UTF-8
Content-encoding: Gzip
Vary: Accept-Encoding
Server: Microsoft-Microsoft IIS/7.5
X-ASPnet-version: 2.0.50727
X-powered-by: ASP. NET
Date: Sun, 01 Nov 2009 12:12:45 GMT
Content-Length: 489
And the URL in the address bar is positioned to the http://xml.test.com/redirect.aspx
Let's take a look at the execution on the request page.
Public partial class HttpStatus: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
Server. Transfer ("/redirect. aspx ");
}
}
The HTTP request information is
Http://xml.test.com/HttpStatus.aspx
GET/HttpStatus. aspx HTTP/1.1
Host: xml.test.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv: 1.9.0.14) Gecko/2009082707 Firefox/3.0.14
Accept: text/html, application/xhtml + xml, application/xml; q = 0.9, */*; q = 0.8
Accept-Language: zh-cn, zh; q = 0.5
Accept-Encoding: gzip, deflate
Accept-Charset: gb2312, UTF-8; q = 0.7, *; q = 0.7
Keep-Alive: 300
Connection: keep-alive
HTTP/1.x 200 OK
Cache-Control: private
Content-Type: text/html; charset = UTF-8
Content-Encoding: gzip
Vary: Accept-Encoding
Server: Microsoft-Microsoft IIS/7.5
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP. NET
Date: Sun, 01 Nov 2009 12:17:21 GMT
Content-Length: 489
The comparison between the two different requests clearly shows that the http request is completely different.
And the current URL is still in progress.
Google also has some suggestions on searching. Do not use 302 redirection as much as possible. Redirect will generate a targeted 302 status, which is slightly different on the performance layer, but may be somewhat delayed for search engines.
Let's take a look at GOOGLE's explanation.
If you need to change the URL of a page as it is shown in search engine results, we recommended that you use a server-side 301 redirect. this is the best way to ensure that users and search engines are directed to the correct page. the 301 status code means that a page has permanently moved to a new location.
Http://www.google.com/support/webmasters/bin/answer.py? Hl = en & answer = 93633
In addition, Server. Execute is rarely used, and its function is to reverse the content of the jump request page back to the current URL.
HttpStatus. aspx
Public partial class HttpStatus: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
Server. Execute ("/redirect. aspx ");
}
}
Redirect. aspx
Public partial class redirect: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
Response. Write ("Server. Execute ");
}
}
When you directly request HttpStatus. aspx, the page displays Server. Execute, which directly adds the value of the user request page to the current page. Its HTTP request information is as follows:
Http://xml.test.com/HttpStatus.aspx
GET/HttpStatus. aspx HTTP/1.1
Host: xml.test.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv: 1.9.0.14) Gecko/2009082707 Firefox/3.0.14
Accept: text/html, application/XHTML + XML, application/XML; q = 0.9, */*; q = 0.8
Accept-language: ZH-CN, ZH; q = 0.5
Accept-encoding: gzip, deflate
Accept-charset: gb2312, UTF-8; q = 0.7, *; q = 0.7
Keep-alive: 300
Connection: keep-alive
Cache-control: Max-age = 0
HTTP/1.x 200 OK
Cache-control: Private
Content-Type: text/html; charset = UTF-8
Content-encoding: Gzip
Vary: Accept-Encoding
Server: Microsoft-Microsoft IIS/7.5
X-ASPnet-version: 2.0.50727
X-powered-by: ASP. NET
Date: Sun, 01 Nov 2009 12:32:03 GMT
Content-Length: 552
For Google's discussion, we use server-side redirection. server. transfer is the server-side jump, and redirect is the client-side request jump. But when will redirect be used to use server. transfer?
Summary:
Use redirect to redirect a user to a page on another server.
When you need to redirect a user to a non-aspx page, for example, html uses redirect
When the query string needs to be retained to the server as part of the url, because the other two methods cannot achieve two postback operations, the data is first brought back to the server using redirect
Switch between aspx pages (logon not involved) Using transfer
By the way, how to use the redirect method to use Chinese Characters in query strings is often garbled because URLs do not support Chinese characters. In this case, the conversion is required:
String message = server. urlencode ("Liang Yuanhua blog ");
Controls on. NET
I. server controls
HyperLink
Use the NavigateUrl attribute to specify the url to jump.
It is mainly used by the user to determine when to convert pages, but the program is used to control the conversion target.
Ii. Program Control
1. Response. Redirect ()
Server -------- notify client ---------- request jump to server
It is mainly used to pass the query string as part of the url to another page, or the page to jump to is an html page
2. Server. Transfer ()
The biggest feature of this method is that the url is still the url of the page before the jump.
It is mainly used to transfer the execution process to another aspx page of the same web server. Because it is retained after the jump
Request and session values, and the data on the page before the jump can be used.
Another value is:
3. Server. Execute ()
After this method is executed, the program control will return the next generation code of the jump code, which is equivalent to our modal window.
The Return Value of the redirected page.
Sever.exe cute this method is mainly used in the page design, and he must jump to the page under the same site. This method needs to be used to insert the output results of a page to another ASPX page. Most of them are in the table, where a page is nested to another page.
Use redirect to redirect a user to a page on another server.
Execute is used to insert the output result of the ASPX page to another ASPX page.
· If you want to capture the output result of An ASPX page and insert the result to a specific location of another ASPX page, use server. Execute.
· To ensure that the HTML output is valid, use response. Redirect instead of server. Transfer or server. Execute.
Address: http://www.cnblogs.com/Leung/archive/2009/11/01/1594061.html