asp.net webconfig下的httphandler模块配置

搞了半天的结果。。

//system.web下

//HoWave.Web为项目名称,HandlePictrue为 实现类
        
            
     
     
     
     
     
     
     
     
        


    
        
            
                
            

        

        
       
           
           
           
       

       
           
       

   
    


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

namespace HoWave.Web
{
    public class HandlePictrue:IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            string requesthost = (context.Request.UrlReferrer == null ? "" : context.Request.UrlReferrer.Host);
            string picturehost = context.Request.Url.Host;
            
            string relationPath = context.Request.FilePath.ToLower();
            if (relationPath.EndsWith(".jpg") || relationPath.EndsWith(".jpeg") || relationPath.EndsWith(".png") || relationPath.EndsWith(".bmp") || relationPath.EndsWith(".gif"))
            {
                context.Response.ContentType = "image/JPEG";
                string absolutePath = context.Server.MapPath(context.Request.FilePath);
                if (requesthost != picturehost)//盗链,返回提示图片
                {
                    context.Response.WriteFile("/Img/linknotice/ImageForbiden.jpg");
                }
                else if (!File.Exists(absolutePath))//图片不存在,返回提示图片
                {
                    context.Response.WriteFile("/Img/linknotice/ImageNotFound.jpg");
                }
                else
                {
                    context.Response.WriteFile(relationPath);
                }
            }
            //else context.RewritePath(relationPath);

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

    }
}



你可能感兴趣的:(C#/.NET/VS/Linq)