重定向实例

时间:2014-05-09 12:20:14   收藏:0   阅读:243

1.Server.Transfer

bubuko.com,布布扣
        //内部重定向到静态资源
        protected void Button1_Click(object sender, EventArgs e)
        {
            Server.Transfer("~/img/1.jpg");
            Response.Write("此后的代码不会执行");

            string filename = LocalPathHelper.DesktopPath + "/text.txt";
            FileStream stream = File.Create(filename);
            stream.Close();
        }

        //内部重定向到动态页面
        protected void Button2_Click(object sender, EventArgs e)
        {
            //追加参数,url方式
            Server.Transfer("~/view/LoginTwo.aspx?username=admin");


            //追加headers
            NameValueCollection coll = new NameValueCollection();
            coll.Add("adf", "1234");
            Server.TransferRequest("~/view/LoginTwo.aspx", true, "POST", coll);


            //不能请求外域
            //Server.Transfer("http://www.baidu.com");
        }
bubuko.com,布布扣

2.Response.Redirect

bubuko.com,布布扣
        //请求外域
        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("http://www.baidu.com");
        }

        //带参数传递
        protected void Button2_Click(object sender, EventArgs e)
        {
            //url方式指定参数
            Response.Redirect("~/view/LoginTwo.aspx?username=admin");

            //永久重定向
            Response.RedirectPermanent("~/view/LoginTwo.aspx?username=admin");
        }

        //请求静态资源
        protected void Button3_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/img/1.jpg");
        }
bubuko.com,布布扣

 

 

重定向实例,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!