您的位置:首页 > 编程语言 > ASP

ASP.net页面跳转方式三种

2010-05-05 20:57 696 查看
asp.net在开发时,在不同的页面间跳转是我们常遇到的一件事,当一个复杂的逻辑在一个页面放不下分成二个或多个页面处理就需要在页面间跳转,用的最多还是用户的登陆吧.

ASP.NET用的最多的跳转是Response.Redirect,这个命令可以直接把请求重定向到一个相对或绝对的路径.它会把当前页面的的Http流阻断直接重定向到新的URL.

而Server.Transfer,这个命令是由IIS服务器直接在服务器端执行跳转,这个跳转的路径必须是相对路径,也就是同一个网站下面的PATH或虚拟目眼录.不能是外网的绝对路径,因为直接是由当前请求页面的同一个HTTP句柄指定请求同一站点上的路径.

它们的区别在哪,先看下HTTP 头吧,先看Response.Redirect

protected void Page_Load(object sender, EventArgs e)
{
Response.Redirect("/redirect.aspx"); //直接重定向到本站的其路径上
}

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 302 Found -->注意这个302
Cache-Control: private
Content-Type: text/html; charset=utf-8
Location: /redirect.aspx
Server: 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--> 这个是请求跳转的页面

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-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

而且地址栏的URL是定位到 http://xml.test.com/redirect.aspx
再来看看请求页执行

public partial class HttpStatus : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Server.Transfer("/redirect.aspx");
}
}

得到的HTTP请求信息是
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-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

这二个不同的请求比较很明显示了吧,HTTP REQUEST完全不一样了

而且当前的URL还是停留在http://xml.test.com/HttpStatus.aspx而不是redirect.aspx这个路径了.那么这个命令其实只是在服务器端执行了一个直接获取redirect.aspx文件然后回传数据流给请求页面,而Redirect是相当于用户执行多一次请求,重定向到了redirect.aspx.

Google在搜索这块也是有见议尽量不要用302跳转,Redirect会产生一重定向302状态,表现层上区别不大,但对于搜索引擎来说有些延时了.

看下GOOGLE的解释

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

还有一个用的比较少的是Server.Execute,它的作用是把跳转请求页面的内容反回到当前的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");
}
}

当用户直接请求HttpStatus.aspx时,页面会显示Server.Execute,它会直接把用户请求页面的值加在当前页面中去,它的HTTP请求信息如下
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-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

GOOGLE见议我们用服务器端跳转,Server.Transfer就是服务器端跳转了,Redirect是客户端请求跳转,但什么时候用Redirect什么情况下用Server.Transfer呢?

总结下:

当需要把用户跳转到另一台服务器上的页面的时候 使用redirect
当需要把用户跳转到非aspx页面时候,如html 使用redirect
需要把查询字符串作为url一部分的时候保留传给服务器的时候,因为其他2种方法不能做到2次postback,把数据先带回服务器 使用redirect
需要aspx页面间的转换(不涉及登录) 使用transfer

顺便提一下,如何使用redirect方法在查询字符串中使用汉字,因为经常的情况是出现乱码,原因是url不支持汉字。这个时候需要转换:
string message =server.urlencode("梁远华博客");

在.NET上面控件

一、服务器控件
HyperLink
通过NavigateUrl属性来指定要跳转的url。
主要用于由用户来决定何时转换页面,但是用程序来控制转换的目标。
二、程序控制
1、Response.Redirect()
服务器--------》通知客户端----------》请求跳转到服务器端
主要用于需要将查询字符串作为url的一部分传递给另一页面,或跳转的页面是html页面
2、Server.Transfer()
使用此方法最大的特点是url仍然是跳转前的页面的url。
主要用于需要将执行流程转到同一web服务器的另一aspx页面时用。因为它在跳转后保留了
request和session值,并且可以使用跳转前页面的数据。

还有一个值的一说的是
3、Server.Execute()
此方法执行完后程序控制权会返回到跳转代码的下一代码,相当于我们的模态窗口,此时可以得到
跳转过页面的返回值。
sever.execute 这个方法主要是用在页面设计上面,而且他必须是跳转同一站点下的页面。这个方法是需要将一个页面的输出结果插入到另一个aspx页面的时候使用,大部分是在表格中,将某一个页面类似于嵌套的方式存在于另一页面。
当需要把用户跳转到另一台服务器上的页面的时候 使用redirect
当需要把aspx页面的输出结果插入到另一个aspx页面的时候使用 execute方法。
·如果要捕获一个ASPX页面的输出结果,然后将结果插入另一个ASPX页面的特定位置,则使用Server.Execute。
·如果要确保HTML输出合法,请使用Response.Redirect,不要使用Server.Transfer或Server.Execute方法。

/article/5281212.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: