<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
if (context.Request.UrlReferrer.Host == "10.11.43.52")
{
context.Response.Expires = 0;
context.Response.Clear();
context.Response.ContentType = "image/jpg";
context.Response.WriteFile(context.Request.PhysicalPath);
context.Response.End();
}
else
{
context.Response.Expires = 0;
context.Response.Clear();
context.Response.ContentType = "image/jpg";
context.Response.WriteFile(context.Request.PhysicalApplicationPath+"error.jpg");
context.Response.End();
}
}
public bool IsReusable {
get {
return true;
}
}
}
1、一般處理程序文件(VS2008下可以直接編譯通過),如果是VS2005,則須寫入App_code然後進行編譯
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Handler 的摘要說明
/// </summary>
public class Handler : IHttpHandler {
public Handler()
{
//
// TODO: 在此處添加構造函數邏輯
//
}
public void ProcessRequest (HttpContext context) {
if (context.Request.UrlReferrer.Host == "10.11.43.52")
{
context.Response.Expires = 0;
context.Response.Clear();
context.Response.ContentType = "image/jpg";
context.Response.WriteFile(context.Request.PhysicalPath);
context.Response.End();
}
else
{
context.Response.Expires = 0;
context.Response.Clear();
context.Response.ContentType = "image/jpg";
context.Response.WriteFile(context.Request.PhysicalApplicationPath+"error.jpg");
context.Response.End();
}
}
public bool IsReusable {
get {
return true;
}
}
}
2、web.config文件配置如下:對handler內容進行處理,攔截
<?xml version="1.0"?>
<!--
注意: 除了手動編輯此文件以外,您還可以使用
Web 管理工具來配置應用程序的設置。可以使用 Visual Studio 中的
“網站”->“Asp.Net 配置”選項。
設置和註釋的完整列表在
machine.config.comments 中,該文件通常位於
\Windows\Microsoft.Net\Framework\v2.x\Config 中
-->
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<!--
設置 compilation debug="true" 將調試符號插入
已編譯的頁面中。但由於這會
影響性能,因此只在開發過程中將此值
設置爲 true。
-->
<compilation debug="true"/>
<!--
通過 <authentication> 節可以配置 ASP.NET 使用的
安全身份驗證模式,
以標識傳入的用戶。
-->
<authentication mode="Windows"/>
<!--
如果在執行請求的過程中出現未處理的錯誤,
則通過 <customErrors> 節可以配置相應的處理步驟。具體說來,
開發人員通過該節可以配置
要顯示的 html 錯誤頁
以代替錯誤堆棧跟蹤。
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
<httpHandlers>
<add verb="*" path="*.jpg" type="Handler"/>
</httpHandlers>
</system.web>
</configuration>
3、配置IIS,對IIS的asp.net處理內容進行過濾,對.jpg文件指定同aspx相同的dll
頁面代碼:
<%@ Page Language="C#" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>無標題頁</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<img src="j.jpg" /> </div>
</form>
</body>
</html>