- 热门文章:
- · 使用 sqlserver来存放和取得 session
- · session 和 viewstate 的比较
- · Online CPU Console using a Web Control Library with .NET Security(3)
- · Online CPU Console using a Web Control Library with .NET Security(2)
- · Online CPU Console using a Web Control Library with .NET Security(1)
- · 实现由web.config控制的验证
- · Passport 你的网站(在你的WebSite上实现MS Passport )上
- · Passport 你的网站(在你的WebSite上实现MS Passport )下
- · 利用ASP.NET来访问Excel文档
- · 如何把存储在数据库中的图片根据自己的需要的大小显示出来。【转】
- · Office XP Charting Examples in asp.net
- · 自定义控件的使用二----DBGlobal.cs部分
web.config 关于HttpHandlers 和HttpModules的使用实例【转】
Introduction
ASP.NET allows you to extend its functionality in two main ways :
HttpHandlers
HttpModules
Http handlers are special applications that typically handle files with certain extension. For example when you request a file with extension .asp IIS routes it to ASP processor. But what if I want to handle files with my own extensions say .bipin? Http handlers allow us to do just that. Now, you might be thinking what is the use of a new file extension? Consider a case where you want to generate graphics on the fly. In such cases you will write an Http handler that will do your task. Note that Http handlers are typically called for the specific file extensions they are designed for. If you have worked with ISAPI extensions in past you will find this concept very familiar.
Http modules are similar to Http handlers in that they allow you to tweak with the request and response. However, they are typically executed for every request irrespective of the file type. If you have worked with ISAPI filters before you will find this concept very familiar.
In this article we will see how to create Http Handlers and Http Modules and use them in your ASP.NET pages.
IHttpHandler interface
In order to create Http handler you have to create a class that implements IHttpHandler interface. This interface has one method with following signature :
Sub ProcessRequest(Context as HttpContext)
It also has one read only property with following signature :
Public ReadOnly Property IsReusable() As Boolean
The ProcessRequest method is used to do all of your processing. The context parameter provides access to various objects like Request and Response. The IsReusable property tells whether another requests can use the same instance of Http handler.
Creating the class implementing IHttpHandler
Following is a class that implements IHttpHandler interface.
Public Class MyHttpHandler
Implements IHttpHandler Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest context.Response.Write("hello world") End Sub Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable Get Return True End Get End Property End Class
Here, we are simply outputting a string "Hello World" for each request handled by this Http handler. You can perform any complex task as per your requirements.
Configuring our Http handler
After you create your Http handler class you should configure your web application so that specific requests will be handled by the handler. To accomplish this you will modify web.config file as follows :
<httpHandlers>
<add verb="*" path="hello.aspx"
type="SampleWebApplication.MyHttpHandler,SampleWebApplication" />
</httpHandlers>
Here, verb attribute indicates GET, POST or * (all). The path attribute indicates the resource to be handled. In our case we have specific file hello.aspx. Type attribute indicates the fully qualified name of the class and name of assembly respectively.
In case you have to handle different extension say *.bipin then in addition to configuring in web.config (as shown above) you also need to add the extension in IIS. This allows IIS to forward request for specific extension to ASP.NET processor which in turn forwards it to your Http handler.
Testing your http handler
In order to test your Http handler simply add a page named hello.aspx in the project and run it in the browser. You should get "Hello world" displayed in your browser.
IHttpModule interface
In order to create a HttpModule you will first create a class that implements IHttpModule interface. This interface provides following two methods that you must implement :
Sub Init(ByVal app As HttpApplication)
Sub Dispose()
Out of the above two methods the Init() method is of our interest. This method receives an instance of HttpApplication that represents the current application instance. You will attach various event handlers in this method as we will see later on.
Creating the class implementing IHttpModule
Now, let us create a class that implements IHttpModule interface. Here is the complete code for the class :
Public Class MyHttpModule
Implements IHttpModule
Public Sub Init(ByVal app As HttpApplication)
Implements IHttpModule.Init
AddHandler app.BeginRequest, AddressOf MyBeginRequest
AddHandler app.EndRequest, AddressOf MyEndRequest
End Sub
Public Sub Dispose() Implements IHttpModule.Dispose
End Sub
Public Sub MyBeginRequest
(ByVal s As Object, ByVal e As EventArgs)
Dim app As HttpApplication
app = CType(s, HttpApplication)
app.Response.Write("Hello begin request")
End Sub
Public Sub MyEndRequest
(ByVal s As Object, ByVal e As EventArgs)
Dim app As HttpApplication
app = CType(s, HttpApplication)
app.Response.Write("Hello end request")
End Sub
End Class
Note how we have used Init() method to attach event handlers to application events. In our example we have set MyBeginRequest method to handle BeginRequest event of HttpApplication and MyEndRequest method handles EndRequest event. This will cause the every request to output "Hello begin request" and "Hello end request" at the start and end of the page respectively.
Add module details in web.config
Prior to using the module we just developed we must inform IIS and ASP.NET abut it. The place to do that is web.config file. Add following section to the file :
<httpModules>
<add type="SampleWebApplication.MyHttpModule,
SampleWebApplication"
name="MyHttpModule" />
</httpModules>
The <httpModules> section is used to publish information about our module. All the modules from this section are loaded by ASP.NET at run time. The type attribute specifies the fully qualified class name and assembly name respectively.
Testing your http module
In order to test our module, create a test web form and put some controls on it. (Remember that if you use Grid layout our messages may not be displayed exactly at the beginning and end. For our testing switch to Flow layout). Now run the web form. You should see our messages at the top and bottom of the web form.
下一篇:使用 sqlserver来存放和取得 session >>
相关文章:
- · 自定义控件的使用例子一
- · 任意在datagrid里面添加控件。
- · 三、ASPNET中实现在线用户检测(使用后台守护线程)
- · 二、ASPNET中实现在线用户检测(使用后台守护线程)
- · ASPNET中实现在线用户检测(使用后台守护线程)
- · 通过自定义类来达到向aspx页面加入脚本代码的例子
- · 一个实现自动求和/合并单元格/排序的DataGrid
- · WINDOWS2000服务器账号登陆身份验证
- · .NET中带有口令加密的注册页面(还是发表在这里吧~-~)
- · .NET中带有口令加密的注册页面
- · ASP.NET验证控件祥解(转)
- · 改写我前面的即时消息的发送,包含同时给多人发送信息!
- · 利用.net来发送即时消息:)
- · 个性化查询(具有分类模糊查询、换页等功能)
- · .net中即时消息发送的实现……
- · 用vb.net做的校友录……(附所有源代码)
- · web.config一个中文解释
- · Application事件的执行顺序
- · asp.net中当服务器出错时显示指定的错误页面,同时把错误信息写入系统日志文件的探讨
- · 完整的网站间共享数据的WebService(Love.NET原创)
- · HOW TO: Create an Assembly with a Strong Name
- · 图象显示和翻转控件(用户自定义控件)--我也来凑凑热闹--(转)
- · web组件的通信---浅谈事件
- · Using DropDownList control in DataGrid
- · KW大师精品文章赏析
- · 实现一个客户端的DataSet-----index.htm
- · 实现一个客户端的DataSet-----ClientDataSetDataProvider.asmx.cs(1)
- · 实现一个客户端的DataSet-----ClientDataSetDataProvider.asmx.cs(2)
- · 实现一个客户端的DataSet-----ClientDataSet.htc
- · 安装framework以后出现不能显示aspx页面,提示用户名和密码不匹配问题的解决(chicken修改补充)
- · IIS5 HTTP500内部错误解决办法(转自eNet)------(一)
- · IIS5 HTTP500内部错误解决办法(转自eNet)------(二)
- · IIS5 HTTP500内部错误解决办法(转自eNet)-------(三)
- · page_Load和page_Init的区别
- · 关于如何添加一个自增的列【原创】
- · 获得存储过程返回值的方法(return的值)
- · 关于返回前面的页面。如何两者兼得,自问自答
- · 在ASP.NET中的变量数值管理------看了这个我基本上对原来的REQUEST.FORM的方法传递变量绝望了
