zoukankan      html  css  js  c++  java
  • 自定义 HttpModule 示例

    下面的自定义模块只是在任何 HTTP 请求的开头返回一个 Web 页消息和并在处理完该请求后返回另一个 Web 页消息。下面的 Init 函数为两个 HttpApplication 事件 BeginRequestEndRequest 注册事件处理程序。每个事件处理程序都编写为模块的私有方法。当已注册的事件被引发时,ASP.NET 将调用适当的处理程序方法,该方法写一个 Web 页然后返回。

    [C#]
    using System;
    using System.Web;
    using System.Collections;
    public class HelloWorldModule : IHttpModule {
    public String ModuleName {
    get { return "HelloWorldModule"; }
    }
    // In the Init function, register for HttpApplication
    // events by adding your handlers.
    public void Init(HttpApplication application) {
    application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
    application.EndRequest += (new EventHandler(this.Application_EndRequest));
    }
    // Your BeginRequest event handler.
    private void Application_BeginRequest(Object source, EventArgs e) {
    HttpApplication application = (HttpApplication)source;
    HttpContext context = application.Context;
    context.Response.Write("<h1><font color=red>HelloWorldModule: Beginning of Request</font></h1><hr>");
    }
    // Your EndRequest event handler.
    private void Application_EndRequest(Object source, EventArgs e) {
    HttpApplication application = (HttpApplication)source;
    HttpContext context = application.Context;
    context.Response.Write("<hr><h1><font color=red>HelloWorldModule: End of Request</font></h1>");
    }
    public void Dispose()
    {
    }
    }
    [Visual Basic]
    Imports System
    Imports System.Web
    Imports System.Collections
    Public Class HelloWorldModule
    Implements IHttpModule
    Public ReadOnly Property ModuleName() As [String]
    Get
    Return "HelloWorldModule"
    End Get
    End Property
    ' In the Init function, register for HttpApplication
    ' events by adding your handlers.
    Public Sub Init(ByVal application As HttpApplication) Implements IHttpModule.Init
    AddHandler application.BeginRequest, AddressOf Me.Application_BeginRequest
    AddHandler application.EndRequest, AddressOf Me.Application_EndRequest
    End Sub
    ' Your BeginRequest event handler.
    Private Sub Application_BeginRequest(ByVal [source] As [Object], ByVal e As EventArgs)
    Dim application As HttpApplication = CType([source], HttpApplication)
    Dim context As HttpContext = application.Context
    context.Response.Write("<h1><font color=red>HelloWorldModule: Beginning of Request</font></h1><hr>")
    End Sub
    ' Your EndRequest event handler.
    Private Sub Application_EndRequest(ByVal [source] As [Object], ByVal e As EventArgs)
    Dim application As HttpApplication = CType([source], HttpApplication)
    Dim context As HttpContext = application.Context
    context.Response.Write("<hr><h1><font color=red>HelloWorldModule: End of Request</font></h1>")
    End Sub
    Public Sub Dispose() Implements IHttpModule.Dispose
    End Sub
    End Class

    如下所示注册该模块:

    <configuration>
    <system.web>
    <httpModules>
    <!-- <add name="HelloWorldModule"
    type="HelloWorldModule, HelloWorldModule" /> -->
    </httpModules>
    </system.web>
    </configuration>
  • 相关阅读:
    使用Pencil进行UI草图设计
    模板机制在Zend Framework
    数组/链表高效去重(算法题
    Docker常用命令总结
    vscode中的git使用
    二分搜索模板
    多年没有管理的技术博客了,即日起开始管理起技术博客
    c# office不同版本下中使用Excel
    最近在忙项目,好久不来
    中秋 国庆
  • 原文地址:https://www.cnblogs.com/wzyexf/p/364357.html
Copyright © 2011-2022 走看看