asp.net学习——Response对象

 

  (2011-03-29 07:33:03)
标签: 

杂谈

分类: asp.net学习
  • 响应的缓冲输出:为了提高服务器的性能,asp.net向浏览器Write的时候默认并不会每Write一次都会立即输出到浏览器,而是会缓存数据,到合适的时机或者响应结束才会将缓冲区中的数据一起发送到浏览器。
  • Response对象的主要成员:
    • Response.Buffer、Response.BufferOutput:两个的属性是一样的,Buffer内部就是调用BufferOutput。用来控制是否采用响应缓存,默认是true。
    • Response.Flush()将缓冲区中的数据发送给浏览器。这在需要将Write出来的内容立即输出到浏览器的场合非常适用。
    • Response.Clear()清空缓冲器中的数据,这样在缓存区中的没有发送到浏览器端的数据被清空,不会被发送到浏览器。
    • Response.ContentEncoding输出流的编码。
    • Response.ContentType输出流的内容类型,比如html(text/html) 、普通文本(text/pain)还是JPEG图片(image/JPEG)。
    • Response.Cookies返回给浏览器的Cookie的集合,可以通过它设置Cookie
    • Response.OutputStream输出流,在输出图片、Excel文件等非文本内容的时候使用它。(ashx中保存图片就用这个)
    • Response.End()终止响应,将之前缓存中的数据发给浏览器,End()之后的代码不会被继续执行。在终止一些非法请求的时候,可以用End()立即终止请求。(不往下执行了)
    • Response.Redirect(url)重定向浏览器到新的网址。即可以重定向到站外网址也可以重定向到站内网址。Redirect是向浏览器发回302重定向,是通知浏览器“请重新访问url这个网址”,这个过程经历了服务器知浏览器“请重新访问url这个网址”和浏览器接到命令访问新网址的过程。用Redirect因为是浏览器自己去访问新网址的,所以在地址栏中是可以看到网址的变化的。 后面用这个来防止刷新时浏览器提示“重试”。
    • Response.SetCookie(HttpCookie cookie),向输出流中更新写到浏览器中的Cookie,如果Cookie存在就更新不存在就增加。是对Response.Cookies的简化调用。
    • Response.Write()向浏览器输出内容。
    • Response.WriteFile(filename)向浏览器输出文件。

Response.Flush例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace asp.net入门2
{
    /// <summary>
    /// Flush练习 的摘要说明
    /// </summary>
    public class Flush练习 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            for (int i = 0; i < 25; i++)
            {
                System.Threading.Thread.Sleep(500);//模拟处理所需的时间,延迟
                context.Response.Write("第"+i+"步执行完毕 <br />");
                context.Response.Flush();//没有这步的话,要等全部都缓冲好,再发送出来,有了这步是一条一条输出
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

 

Response.Redirect例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace asp.net入门2
{
public partial class Redirect练习 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string q = Request["q"];
if (q == "1")
{
Response.Write("你传的是1");
}
else if (string.IsNullOrEmpty(q))
{
Response.Write("什么都没有");
}
else
{
Response.Redirect("~/image/资料.jpg");
}
}
}
}

asp.net学习——Response对象

asp.net学习——Response对象

 

重定向:
asp.net学习——Response对象

posted @ 2017-04-25 09:49  领悟.海洋  阅读(162)  评论(0编辑  收藏  举报