zoukankan      html  css  js  c++  java
  • asp.net frameworke处理程序的作用

    1 向客户端发送响应的工作都由处理程序完成

    2 任何实现System.web.ihttpHandler接口的类都可以作为传入的http请求的目标

    3 如果需要重复使用自定义处理程序对象,需要创建自定义处理程序工厂。

    4 如何创建自定义处理程序

      

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace Handlers
    {
        public class CustomHandler : IHttpHandler
        {
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
    
            public void ProcessRequest(HttpContext context)
            {
                string time = DateTime.Now.ToShortTimeString();
                if (context.Request.CurrentExecutionFilePathExtension==".json")
                {
                    context.Response.ContentType = "application/json";
                    context.Response.Write(string.Format("{{"time":"{0}"}}", time));
                }
                else
                {
                    context.Response.ContentType = "text/html";
                    context.Response.Write(string.Format("<span>{0}</span>", time));
                }
            }
        }
    }

      在web.config文件中注册自定义的处理程序

    <system.webServer>
    <handlers>
    <add name="customJson" path="*.json" verb="GET" type="Handlers.CustomHandler"/>
    <add name="customText" path="Time.text" verb="*" type="Handlers.CustomHandler"/>
    </handlers>
    </system.webServer>

    5 如何创建自定义的处理程序工厂

    自定义处理程序工厂是实现IHttpHandlerFactory接口的类,他负责生成用于响应的IHttpHandler对象

    首先创建实现IHttpHandlerFactory接口的实例

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace Handlers
    {
        public class InstanceControlFactory : IHttpHandlerFactory
        {
            private int factoryCounter = 0;
            public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
            {
                return new InstanceControlHandler(factoryCounter++);
            }
    
            public void ReleaseHandler(IHttpHandler handler)
            {
                throw new NotImplementedException();
            }
        }
    
        public class InstanceControlHandler : IHttpHandler
        {
            private int v;
    
            public InstanceControlHandler(int v)
            {
                this.v = v;
            }
    
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
    
            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/plain";
                context.Response.Write(string.Format("the counter value is {0}",v));
            }
        }
    }

    其次在web.config文件中注册实现IHttpHandlerFactory接口的类

      <system.webServer>
        <handlers>
          <add name="customJson" path="*.json" verb="GET" type="Handlers.CustomHandler"/>
          <add name="customText" path="Time.text" verb="*" type="Handlers.CustomHandler"/>
          <add name="InstanceControl" path="*.instance" verb="*" type="Handlers.InstanceControlFactory"/>
        </handlers>
      </system.webServer>

    6 如何重复的使用处理程序,将同一个处理程序对象应用于多个不同的请求

  • 相关阅读:
    JAVA 从一个List里删除包含另一个List的数据
    CentOS 常用命令合集
    010---软链接和硬链接
    009---linux进程管理
    008---vim编辑器
    007---归档、压缩、解压缩
    006---Linux用户、群组和权限
    005---Linux文件与目录管理
    001---Linux系统的启动过程
    002---Linux系统目录结构
  • 原文地址:https://www.cnblogs.com/mibing/p/7661490.html
Copyright © 2011-2022 走看看