zoukankan      html  css  js  c++  java
  • ASP.NET中IHttpHandler与IHttpModule的区别(带样例说明)

    IHttpModule相对来说,是一个网页的添加
    IHttpHandler相对来说,却是网页的替换

    先建一个HandlerDemo的类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace Http_Handler_Model
    {
        public class HandlerDemo : IHttpHandler
        {
         /*
                这个IsReusable的true是可以提高效率但是,会线程不安全
                IHttpHandler实例可以再次使用
    
                false,会安全一些,效率会低一些
                IHttpHandler的实例就不能使用 
                 */
            public bool IsReusable => true;
    
            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/html";
                context.Response.Write("<h2>这是HandlerDemo做出来的</h2>");
            }
        }
    }
    

    在建一个Module的类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace Http_Handler_Model
    {
        public class Modules : IHttpModule
        {
            public void Dispose()
            {
            
            }
    
            public void Init(HttpApplication context)
            {
            //添加一个开始的事件,和一个结束的事件
                context.BeginRequest += Context_BeginRequest;
                context.EndRequest += Context_EndRequest;
            }
    
            private void Context_EndRequest(object sender, EventArgs e)
            {
                HttpApplication httpApplication = sender as HttpApplication;
                httpApplication.Response.Write("<h2>这是开始页面</h2>");
            }
    
            private void Context_BeginRequest(object sender, EventArgs e)
            {
                HttpApplication httpApplication = sender as HttpApplication;
                httpApplication.Response.Write("<h2>这是结束页面</h2>");
            }
        }
    }
    

    把这两个类建完了,就是添加到配置文件中
    在这里插入图片描述
    打开你当前项目的这个配置文件
    添加这些
    在这里插入图片描述
    这里面配置了handlers和modules

    type的值是:命名空间.(点)你的类

    path的值是:你的作用域,我这里的作用域是某个文件夹下面的aspx

     <system.webServer>
        <handlers>
          <add name="handlers" path="Handler_Test/*.aspx" verb="*" type="Http_Handler_Model.HandlerDemo"/>
        </handlers>
        <modules>
          <add name="modules" type="Http_Handler_Model.Modules"/>
        </modules>
      </system.webServer>
    

    ModuleDemo的aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ModelsDemo.aspx.cs" Inherits="Http_Handler_Model.ModelsDemo" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                这个是modules的样例
            </div>
        </form>
    </body>
    </html>
    
    

    效果图
    这里添加了刚才的Modules类的操作
    在这里插入图片描述

    某个文件夹下面的aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="HandlerDemo.aspx.cs" Inherits="Http_Handler_Model.Handler_Test.HandlerDemo" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                这是Test被替换的页面
            </div>
        </form>
    </body>
    </html>
    
    

    显示效果
    这里没有代码中的这是Test被替换的页面,而是HandlerDemo的操作
    在这里插入图片描述

    有不明白的欢迎评论

  • 相关阅读:
    SQL Server 文件操作
    约束5:外键约束
    SSIS 对数据排序
    列属性:RowGUIDCol、Identity 和 not for replication
    TSQL 数据类型转换
    HierarchyID 数据类型用法
    租房那些事
    SSIS 延迟验证
    SQL Server 管理全文索引
    SQL Server 全文搜索
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13074724.html
Copyright © 2011-2022 走看看