ASP.NET 高级编程基础第九篇—Response和Server对象

前言: 从这篇博客我们开始讲解Response对象和Server对象的使用,Response对象和Server对象的使用也是很广泛的,比如Response对象的输出信息,Server对象读取文件所在的位置等。

  1. Response对象

(1) 响应的缓冲输入:为了提高服务器的性能,ASP.NET向浏览器Write的时候默认并不会Write一次都会立即输出到浏览器,而是会缓冲数据,到合适的时机或者响应结束才会将缓冲区中的数据一起发送到浏览器端。

(2) Response对象的主要成员

 1) Response.Buffer,Response.BufferOutput,进过Reflector反编译,发现这两个属性是一样的,Buffer内部就是调用的BufferOutput,这个属性用来控制是否采用响应缓存,默认是true。

 2) Response.Flush()将缓冲区中的数据发送给浏览器,这里需要将write出来的内容立即输出到浏览器的场合非常使用,案例:大批量数据的导入,显示正在导入第几条数据,用Thread.Sleep模拟耗时。

 注:新建一个一般处理程序step.aspx,在其中写入如下代码:

 1  context.Response.ContentType = "text/plain";
 2 
 3    for (int i = 0; i <= 20; i++)
 4 
 5    {
 6 
 7        System.Threading.Thread.Sleep(1000);
 8 
 9        context.Response.Write("" + i + "步执行完成<br/>");
10 
11        context.Response.Flush();
12 
13        //if (i == 10)
14 
15        //{
16 
17                   //context.Response.Clear();
18 
19        //}
20 
21    }

 3) Response.clear()清空缓冲区中的数据,这样在缓冲区中的没有发送到浏览器端的数据被清空,不会被发送到浏览器,再用aspx输出非HTML的例子中经常看到用Clear来输出HttpModeld等。

 注:新建一个WebFrom项目tupian.aspx,在其中写入如下代码:   

 1  protected void Page_Load(object sender, EventArgs e)
 2 
 3     {
 4 
 5         Response.ContentType = "image/JPEG";
 6 
 7         Response.Clear();
 8 
 9         string fullpath = Server.MapPath("菲.jpg");
10 
11         using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(fullpath))
12 
13         {
14 
15             bitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
16 
17         }
18 
19     }

 4) Response.contentEncoding,输出流的编码

 5) Response.ContentType输出流的内容类型,比如是:html(text/html)还是普通文本(text/plain)还是JPEG图片(image/JPEG)。

 6) Response.Cookies返回给浏览器的Cookie的集合,可以通过它设置Cookie。

 7) Response.OutPutStream输出流,在输出图片,Excel文件等非文本内容的时候要使用它。

 8) Response.End()终止响应,将之前缓冲中的数据发给浏览器,End()之后的代码不会被继续执行,在终止一些非法操作的时候,比如盗链等可以用End()立即终止请求。

 注:添加一个EndTest.aspx项目,拖放一个DropDownList控件和Button控件,在Page_Load中写入如下代码:

 1     protected void Page_Load(object sender, EventArgs e)
 2 
 3     {
 4 
 5         Response.Write("下面还有一句话");
 6 
 7         Response.End();
 8 
 9         Response.Write("cnblogs是我的最爱,你为什么不显示的,哈哈");
10 
11     }

拖放在页面上的所有控件也是不显示的,只显示第一句话。

 9) Response.Redirect(Url)重定向浏览器到新的网址,既可以重定向到站外网址也可以重定向到站内网址,Response.Redirect(“http://www.baidu.com”),Response.Redirect(“a.html”),Redirect是向浏览器发回302重定向,是通知”浏览器”请重新访问访问URL这个网址,这个过程经历了服务器通知浏览器”请重新访问url这个网址”和浏览器接到命令访问新网址的过程,使用HttpWatch查看整个响应过程的Http报文。用Redirect因为是浏览器自己去重新访问新网址的,所以在地址栏中可以看到网址的的变化,后面会用来防止刷新浏览器时提示”重试”。

注:检测信息的时候能够在地址栏中输入?q=1模拟。

 1   protected void Page_Load(object sender, EventArgs e)
 2 
 3     {
 4 
 5         string q = Request["q"];
 6 
 7         if (q == "1")
 8 
 9         {
10 
11             Response.Write("我是韩迎龙");
12 
13         }
14 
15         else if (string.IsNullOrEmpty(q))
16 
17         {
18 
19             Response.Write("你怎么能是空的呢");
20 
21         }
22 
23         else
24 
25         {
26 
27             Response.Write("恭喜你啦");
28 
29         }
30 
31     }

 10) Response.SetCookie(HttpCookie cookie),向输出流中更新写到浏览器中的Cookie,如果Cookie存在就更新,不存在就增加,是对Response.Cookies的简化调用。

 11) Response.Write() 想浏览器输出内容。

 12) Response.WriteFile(fileName) 向浏览器输出文件,比如:Response.WriteFile(“c:/root.ini”).

 注:在step.ashx中写入如下代码:

1    context.Response.ContentType = "Image/JPEG";
2 
3    string full = context.Server.MapPath("菲.jpg");
4 
5    context.Response.WriteFile(full);

 

  1. Server对象

(1) Server是context的一个属性,是HttpServerUtility类的一个对象。

(2) Server.HtmlDecode(),Server.HtmlEncode(),Server.UrlEncode(),Server.UrlDecode()是对HttpUtility类中相应方法的一个代理调用,个人推荐总是使用HttpUtility,因为有的地方很难拿到Server对象,别把HtmlEncode,UrlEncode混淆了,UrlEncode是处理超链接的,HtmlEncode是处理Html代码的。

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

注:新建一个hello.aspx页面,在redirect.aspx.cs页面中再加入如下代码:

   else if (q == "2")

   {

       Server.Transfer("hello.aspx");

   }

在Hello.aspx.cs的page_load页面中写入如下信息:

Response.Write(Request["q"]);

(4) 使用Server.Transefer不能直接从定向到ashx,否则会报错”执行请求出错”。

(5) 有时候不能拿到HttpContext对象,比如在:Global.ascx中,可以通过HttpContext.Current拿到当前的HttpContext,进而取得Response.Request.Server等。

注释:Response和Server我们就说到这里了,下节博客我们开始讲述Httphandler。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值