15
2013
03

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>

.Net Code:

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




版权声明:
作者:真爱无限 出处:http://www.pukuimin.top 本文为博主原创文章版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接.
« 上一篇下一篇 »

相关文章:

评论列表:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。