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

    }
}



转载于:https://www.cnblogs.com/pukuimin/archive/2013/03/15/2988070.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值