zoukankan      html  css  js  c++  java
  • ASP.NET底层机制 (下) HttpHandler

    1.IHttpHandler接口
        定义了实现一个HttpRequest的处理所必须实现的一些系统约定方法。

        public interface IHttpHandler
        
    {
            
    //其他Request是否可以使用IHttpHandler
            bool IsReusable get; }

            
    //处理HttpRequest
            void ProcessRequest(HttpContext context);
        }

    NET为ASP.NET提供了很多系统默认HttpHandler类,用来适应不同类型的HttpRequest
        比如aspx,在machine.config中是这样定义的:    
            <add verb="*" path="*.aspx" type="System.Web.UI.PageHandlerFactory"/>
                说明遇到aspx的Request,ASP.Net会将其交给System.Web.UI.PageHandlerFactory的HttpHandler类来处理
    如果自己定义了新的HttpHandler,而且在Web.config中指定,则系统只会使用这个新的HttpHandler,而不再使用原先指定的

    2.HttpHandler实现了IHttpHandler接口
        一个aspx页面在HttpHandler容器中的ProcessRequest方法才被系统真正的处理解析——即交给PageHandlerFactory处理,该工厂负责提供一个HttpHandler容器,由其处理HttpRequest

    3.如果要在HttpHandler容器中使用Session,必须要实现IRequiresSessionState接口——这只是一个空接口,一个标记

    using System;
    using System.Web;
    using System.Web.SessionState;

    namespace MyNamespace
    {
        
    public class MyHandler:IHttpHandler,IRequiresSessionState
        
    {
            
    public MyHandler() {}

            
    public bool IsReusable
            
    {
                
    get
                
    {
                    
    return true;
                }

            }


            
    public void ProcessRequest(HttpContext context)
            
    {
                HttpResponse response 
    = context.Response;
                HttpRequest request 
    = context.Request;

                HttpSessionState Session 
    = context.Session;
                Session[
    "test"= "hi";

                response.Write(
    "<b>Hello world!</b>");
                response.Write(Session[
    "test"]);
            }

        }

    }

    同时,还要在Web.config中加上声明:
       <httpHandlers>
            
    <add verb="*" path="*" type="MyNamespace.MyHandler,MyNamespace"></add>
       
    </httpHandlers>

    4.IHttpHandlerFactory
        待续。。。


  • 相关阅读:
    Dumpbin 工具的使用
    ffmpeg Windows下采集摄像头一帧数据,并保存为bmp图片
    directdraw显示yuv视频,出现屏保时,yuv显示不出来,表面丢失
    DirectX截图黑屏的解决办法
    VS2008 Project : error PRJ0019: 某个工具从以下位置返回了错误代码: "正在执行生成后事件..."解决方案
    RoundingMode 几个参数详解
    IDEA导入eclipse项目并部署运行完整步骤(转发)
    Intellij idea操作maven时控制台中文乱码
    java 替换json字符串中间的引号保留两边的引号,避免json校验失败
    分布式ID解决方案
  • 原文地址:https://www.cnblogs.com/Jax/p/912958.html
Copyright © 2011-2022 走看看