011-Server服务器对象属性

时间:2023-03-09 03:06:06
011-Server服务器对象属性

011-Server服务器对象属性

Transfer:第一个页面直接调用第二个页面,执行完第二个页面后不再返回第一个页面,立即响应到客户端浏览器。
Execute:第一个页面直接调用第二个页面,执行完第二个页面后再返回第一个页面执行,最后响应到客户端浏览器。

如果能访问到Server对象,可以直接调用Server对象的UrlEncode()和UrlDecode()。如果访问不到,则可以调用HttpUtility.UrlEncode()和HttpUtility.UrlDecode();

Url编码其实就是把每个汉字都是用utf进行编码,获取byte字节,然后把每个byte字节都转换为16进制后前面加个%

Server是上下文对象context的一个属性,是HttpServerUtility类的一个对象

Server.HtmlDecode()、Server.HtmlEncode() Server.UrlEncode()、 Server.UrlDecode()是对HttpUtility类中相应方法的一个代理调用。推荐总是使用HttpUtility,因为有的地方很难拿到Server对象,而且Server的存在是为以前ASP程序员习惯而留的。
别把HtmlEncode、UrlEncode混了,UrlEncode是处理超链接中的中文问题, HtmlEncode是处理html代码的。还是推荐用HttpUtility.HtmlEncode。

Server.Transfer(path) 服务器端重定向请求,Server.Transfer(“JieBanRen.aspx”)将用户的请求重定向给JieBanRen.aspx处理,是服务器内部的接管(不能重定向到外部网站),浏览器是意识不到这个接管的,不是象Response.Redirect那样经历“通知浏览器‘请重新访问url这个网址’和浏览器接到命令访问新网址的过程”,是一次http请求,因此浏览器地址栏不会变化。因为是内部接管,所以在被重定向到的页面中是可以访问到Request、Cookies等这些来源页面接受的参数的,就像这些参数是传递给他的,而Redirect则不行,因为是让浏览器去访问的。注意Transfer是内部接管,因此不能像Redirect那样重定向到外部网站。 (常考)Response.Redirect就可以重定向到外部网站。
注意:不能内部重定向到ashx,否则会报错“执行子请求出错”

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="t3_Server.Index" %>

 <!DOCTYPE html>

 <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%=Server.MapPath("/") %>
<h1>首页</h1>
<hr/>
<%=Html1 %>
</div>
</form>
</body>
</html>
     public partial class Index : System.Web.UI.Page
{
protected string Html1 { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
//服务器端跳转,客户端浏览器中看不到任何变化
//Server.Transfer("List.aspx"); //客户端跳转,告诉浏览器跳转
//Response.Redirect("List.aspx"); //&gt;&lt;
//Html1 = Server.HtmlEncode("<h1>杨过vs小龙包</h1>");
//Html1 = Server.HtmlDecode(Html1); //作用:不希望在地址栏出现中文时,可以进行Url编码
//Response.Redirect("List.aspx?key=" + Server.UrlEncode("杨过")); Html1 = HttpUtility.HtmlEncode("<h1>杨过vs小龙包</h1>");
Html1 = HttpUtility.HtmlDecode(Html1);
}
}
 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="List.aspx.cs" Inherits="t3_Server.List" %>

 <!DOCTYPE html>

 <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>列表</h1>
</div>
</form>
</body>
</html>