asp.net webconfig下的httphandler模块配置

简介: 搞了半天的结果。。 //system.web下 //HoWave.Web为项目名称,HandlePictrue为 实现类                                                  ...

搞了半天的结果。。

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

    }
}



相关文章
|
7月前
|
开发框架 JSON .NET
ASP.NET Core 自定义配置警告信息
自定义配置警告信息需要在 startup 类中的 ConfigureService 方法中进行配置示例: // 注册 控制器服务 services.AddControllers(configure: setup => { setup.ReturnHttpNotAcceptable = true; ...
47 0
|
4月前
|
IDE 前端开发 JavaScript
【C#】C# 开发环境配置(Rider 一个.NET 跨平台集成开发环境)
【1月更文挑战第26天】【C#】C# 开发环境配置(Rider 一个.NET 跨平台集成开发环境)
|
5月前
|
开发框架 .NET PHP
Web Deploy配置并使用Visual Studio进行.NET Web项目发布部署
Web Deploy配置并使用Visual Studio进行.NET Web项目发布部署
|
5月前
|
XML API 数据库
七天.NET 8操作SQLite入门到实战 - 第六天后端班级管理相关接口完善和Swagger自定义配置
七天.NET 8操作SQLite入门到实战 - 第六天后端班级管理相关接口完善和Swagger自定义配置
|
5月前
|
JSON JavaScript 前端开发
全面的.NET微信网页开发之JS-SDK使用步骤、配置信息和接口请求签名生成详解
全面的.NET微信网页开发之JS-SDK使用步骤、配置信息和接口请求签名生成详解
|
5月前
|
SQL Shell 数据库
七天.NET 8操作SQLite入门到实战 - 第二天 在 Windows 上配置 SQLite环境
七天.NET 8操作SQLite入门到实战 - 第二天 在 Windows 上配置 SQLite环境
|
7月前
|
存储 开发框架 .NET
ASP.NET Core 配置
ASP.NET Core (Startup) StartupASP.NET Core必须包含Startup类。它就像 Global.asax 文件,我们传统的 .NET 应用程序。如名称建议的那样,在应用程序启动时首先执行它。在程序类的Main方法中配置主机时,可以使用**UseStartup()**扩展方法配置启动类。请查看下面的程序类,并重点介绍 WebBuilder.UseStart...
28 0
ASP.NET Core 配置
|
8月前
|
容器
.NET Core - 选项框架:服务组件集成配置的最佳实践
.NET Core - 选项框架:服务组件集成配置的最佳实践
|
8月前
|
存储
.NET Core - 自定义配置数据源:低成本实现定制化配置方案
.NET Core - 自定义配置数据源:低成本实现定制化配置方案
|
8月前
|
JSON 数据格式
.NET Core - 配置绑定:使用强类型对象承载配置数据
.NET Core - 配置绑定:使用强类型对象承载配置数据