asp.net webconfig下的httphandler模块配置

搞了半天的结果。。

//system.web下

//HoWave.Web为项目名称,HandlePictrue为 实现类
        <httpHandlers>
            <add verb="*" path="*.php" type="System.Web.UI.PageHandlerFactory" />
      <!--不要被.net处理的类型-->
      <!--<add verb="*" path="*.html,*.jpg,*.jpeg,*.png,*.bmp,*.gif" type="System.Web.DefaultHttpHandler" />-->
      <!--要被.net处理的类型-->
      <add verb="*" path="*.jpg" type="HoWave.Web.HandlePictrue,HoWave.Web" />
      <add verb="*" path="*.jpeg" type="HoWave.Web.HandlePictrue,HoWave.Web" />
      <add verb="*" path="*.png" type="HoWave.Web.HandlePictrue,HoWave.Web" />
      <add verb="*" path="*.bmp" type="HoWave.Web.HandlePictrue,HoWave.Web" />
      <add verb="*" path="*.gif" type="HoWave.Web.HandlePictrue,HoWave.Web" />
        </httpHandlers>

    <system.webServer>
        <defaultDocument>
            <files>
                <add value="index.aspx" />
            </files>
        </defaultDocument>
        <validation validateIntegratedModeConfiguration="false" />
        <handlers>
            <add name=".net40_jpg_tg" path="*" verb="*" type="System.Web.UI.PageHandlerFactory" resourceType="Unspecified" preCondition="integratedMode" />
            <add name=".net40_jpg_tpf" path="*" verb="*" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="None" preCondition="classicMode,runtimeVersionv4.0,bitness32" />
            <add name=".net40_jpg" path="*" verb="*" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" preCondition="classicMode,runtimeVersionv4.0,bitness32" />
        </handlers>
        <modules>
            <!--<add name="myHandlePictrue" type="HoWave.Web.HandlePictrue,HoWave.Web" preCondition="managedHandler" />-->
        </modules>
    <!--程序池要设置为“经典”模式-->
    </system.webServer>

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; }
        }
       

    }
}



你可能感兴趣的:(asp.net webconfig下的httphandler模块配置)