使用Ihttphandler防止盗链图片

来自Asp.net精英论坛:bbs.1aspx.com 


using System;
using System.Web;
using System.Drawing;

namespace ArgumentsTest
{
 /// <summary>
 /// RefImage 的摘要说明。
 /// </summary>
 public class RefImage :IHttpHandler
 {
  #region IHttpHandler 成员

  public void ProcessRequest(HttpContext context)
  {
   if(context.Request.Url.ToString().ToLower().IndexOf("localhost") <= 0 )
   {
    context.Response.Clear();
    context.Response.WriteFile(@"C:/Inetpub/wwwroot/ArgumentsTest/images/error.gif");
    return;
   }
   context.Response.WriteFile(context.Request.RawUrl);
  }

  public bool IsReusable
  {
   get
   {
    // TODO:  添加 RefImage.IsReusable getter 实现
    return false;
   }
  }
  #endregion
 }
}

 <system.web>
 <httpHandlers >
   <add verb="*"
              path="*.gif"
              type="ArgumentsTest.RefImage,ArgumentsTest"/>
 </httpHandlers>


11111

1

22222

2

333333

3

444444

4

555555

5

666666

6

777777

7

888888

8

9999

9

10000

10

11

11

12

12

private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   Set();
  }

  void Set()
  {
   HtmlTableRow[] rows = new HtmlTableRow[]{cell1,cell2,cell3,cell4,cell5,cell6};
   //显示或是隐藏行
   for(int i=1;i<7;i++)
   {
    Label  l1 =(Label) Page.FindControl("lblCustomColumn"+(2*i-1).ToString()) ;
    Label  l2 =(Label) Page.FindControl("lblCustomColumn"+(2*i).ToString()) ;
    rows[i-1].Visible = l1.Visible||l2.Visible;
    if(rows[i-1].Visible&& l1.Visible!=l2.Visible&&l2.Visible)
    {
     HtmlTableCell temp1;
     HtmlTableCell temp2;
     temp1 = rows[i-1].Cells[2];
     temp2 = rows[i-1].Cells[3];
     rows[i-1].Cells.RemoveAt(2);
     rows[i-1].Cells.RemoveAt(2);
     rows[i-1].Cells.Insert(0,temp2);
     rows[i-1].Cells.Insert(0,temp1);
    }
   }
   //*将只有一列显示的的上移
   for(int i=0;i<6;i++)
   {
    if(!rows[i].Visible)continue;
    Label l1 = rows[i].Cells[0].Controls[0] as Label;
    Label l2 = rows[i].Cells[2].Controls[0] as Label;
    if(l2.Visible==l1.Visible)continue;
    int j = i+1 ;
    bool change = false;
    do
    {
     if(j >5 )
     {
      break;
     }
     if(!rows[j].Visible)
     {
      j++;
      continue;
     }
     Label l3 = rows[j].Cells[0].Controls[0] as Label;
     Label l4 = rows[j].Cells[2].Controls[0] as Label;
     if(l3.Visible == l4.Visible)
     {
      j++;
      continue;
     }
     TextBox t3 =  rows[j].Cells[1].Controls[0] as TextBox;
     rows[i].Cells[2].Controls.Add(l3);
     rows[i].Cells[3].Controls.Add(t3);
     rows[j].Visible = false;
     Response.Write( i.ToString() + "与"+j.ToString()+"交换位置 <br>" );
     change = true;

    }while(!change);
   }
  } 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值