日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Asp.net中的HttpModule和HttpHandler的简单用法

發布時間:2023/12/4 编程问答 63 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Asp.net中的HttpModule和HttpHandler的简单用法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在Asp.net中,HttpModule和HttpHandler均可以截取IIS消息進行處理,這使得我們制作人員能夠非常方便的進行諸如圖片水印添加,圖片盜鏈檢查等功能。

下面先就HttpModule的使用方法進行簡單說明:

using System;
using System.Web;

namespace MyWebApp
{
public class MyHttpModule:IHttpModule
{
public void Init(HttpApplication application)
{
application.BeginRequest
+=new EventHandler(application_BeginRequest);
}

public void application_BeginRequest(object sender, EventArgs e)
{
HttpContext context
= (sender as HttpApplication).Context;
context.Response.Write(
"這一部分是由HttpModule添加!<br><script>alert('測試腳本標簽')</script>");
}

#region IHttpModule 成員

void IHttpModule.Dispose()
{
throw new NotImplementedException();
}

#endregion
}
}

需要說明的是,使用HttpModule的時候需要繼承自IHttpModule接口,然后需要實現Dispose成員。需要注意一點的是,這些操作還得在web.config中進行配置,才能夠正常使用:

<!--下面這里是添加的自定義的HTTPModule-->
<add name="MyHttpModule" type="MyWebApp.MyHttpModule"/>

而對于HttpHandler,則需要繼承自IHttpHandler接口,并且也需要在web.config中進行注冊:

using System.Web;
using System.Web.Services;

namespace MyWebApp
{
/// <summary>
/// $codebehindclassname$ 的摘要說明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo
= WsiProfiles.BasicProfile1_1)]
public class MyHttpHandler : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType
= "text/plain";
context.Response.Write(
"這一部分是由HttpHandler添加!");
}

public bool IsReusable
{
get
{
return false;
}
}
}
}

在web.config中的注冊如下:

<!--下面是添加的自定義HTTPHandler-->
<add verb="*" path="*.aspx" type="MyWebApp.MyHttpHandler"/>

希望對你有用。

總結

以上是生活随笔為你收集整理的Asp.net中的HttpModule和HttpHandler的简单用法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。