asp.net - 在 asp.net 开发服务器上调试 httpmodule

标签 asp.net httpmodule

我想在我的 asp.net 应用程序(v 3.5,visual studio 2008)中集成一些 http 模块,但我不确定在运行 web 时触发的 asp.net 开发服务器中调试时如何调试或使用这些模块应用程序。

我需要在解决方案中包含模块源还是可以将 DLL 放入 BIN 中?我来自 1.1 世界,还不习惯 asp.net 开发服务器。

最佳答案

按照以下步骤添加 HTTP 模块:

  • 创建一个名为 MyModule 的新 Visual Studio .NET C# 类库项目.
  • 设置对 System.Web.dll 的引用部件。
  • 将以下指令添加到类中:
    using System.Web;                    
    
  • 重命名类 SyncModule.cs ,然后更改类定义以反射(reflect)这一点。
  • 实现 IHttpModule界面。您的类定义应如下所示:
    public class SyncModule : IHttpModule                    
    
  • 决定您将订阅哪些事件。以下列表概述了 HttpApplication 中的可用事件。您可以订阅的对象:
  • AcquireRequestState : 调用此事件,允许模块获取或创建请求的状态(例如, session )。
  • AuthenticateRequest :当安全模块需要在处理请求之前对用户进行身份验证时调用此事件。
  • AuthorizeRequest : 当请求需要被授权时,由安全模块调用此事件。认证后调用。
  • BeginRequest :调用此事件以通知模块新请求正在开始。
  • Disposed :调用此事件以通知模块应用程序由于某种原因而结束。允许模块执行内部清理。
  • EndRequest :调用此事件通知模块请求正在结束。
  • Error :调用该事件通知模块在请求处理过程中发生的错误。
  • PostRequestHandlerExecute :调用此事件通知模块处理程序已完成对请求的处理。
  • PreRequestHandlerExecute :调用此事件通知模块该请求的处理程序即将被调用。
  • PreSendRequestContent :调用该事件通知模块内容即将发送给客户端。
  • PreSendRequestHeaders : 调用该事件通知模块HTTP头即将发送给客户端。
  • ReleaseRequestState :调用此事件以允许模块释放状态,因为处理程序已完成处理请求。
  • ResolveRequestCache : 认证后调用此事件。缓存模块使用此事件来确定请求是否应由其缓存处理,或者处理程序是否应处理该请求。
  • UpdateRequestCache :在处理程序响应后调用此事件。缓存模块应该使用响应更新它们的缓存。
  • 实现IHttpModule接口(interface)的Init和Dispose方法如下:
    public void Init(HttpApplication app)
    {
       app.BeginRequest += new EventHandler(OnBeginRequest);
    }
    public void Dispose(){ }
    
  • 为事件创建委托(delegate),如下所示:
    public delegate void MyEventHandler(Object s, EventArgs e);    
    
  • 定义 MyEventHandler 类型的私有(private)局部变量持有对事件的引用:
    private MyEventHandler _eventHandler = null;                    
    
  • 创建一个事件,将委托(delegate)连接到 Global.asax 文件或继承自 HttpApplication 的类中的方法。目的:
    public event MyEventHandler MyEvent
    {
       add { _eventHandler += value; }
       remove { _eventHandler -= value; }
    }
    
  • 创建 OnBeginRequest方法,它连接到 BeginRequest HttpApplication的事件:
    public void OnBeginRequest(Object s, EventArgs e)
    {
       HttpApplication app = s as HttpApplication;
       app.Context.Response.Write("Hello from OnBeginRequest in custom module.<br>");
       if(_eventHandler!=null)
          _eventHandler(this, null);
    }        
    
  • 编译项目

  • 来源:http://support.microsoft.com/kb/307996

    将 HTTP 模块添加到您的 web.config将如下所示:
    <system.web>
        <httpModules>
           <add name="CustomHttpModule" type="MyCustomHttpModule"/>
        </httpModules>
    </system.web>
    

    关于asp.net - 在 asp.net 开发服务器上调试 httpmodule,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/679559/

    相关文章:

    ASP.NET 404(未找到页面)重定向并保留原始参数

    html - 如何在 Outlook 中实现 Open Sans Web 字体?

    asp.net - mySQL asp 角色提供程序错误。无法连接到 SQL Server 数据库

    c# - HttpModule.Init - 在 IIS7 集成模式下安全地添加 HttpApplication.BeginRequest 处理程序

    iis - 将所有裸域 url 重定向到保留 url 的子域 (www) url,IIS/ASP.NET 上的一页除外

    c# - MVC 模型 bool 值显示是或否

    c# - 使用母版页的自定义错误处理程序

    c# - 在 IIS7 中不起作用的 HttpModule

    ASP.NET HttpModule : detect first request in a session

    jquery - 单击使用 jquery 将类添加到 aspx linkbutton