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的操作
在这里插入图片描述

有不明白的欢迎评论

发布了1855 篇原创文章 · 获赞 3万+ · 访问量 576万+

猜你喜欢

转载自blog.csdn.net/a1439775520/article/details/105737447