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 如何重复的使用处理程序,将同一个处理程序对象应用于多个不同的请求

  • 相关阅读:
    C#中的扩展方法详解
    MVC Razor模板引擎 @RenderBody、@RenderPage、@RenderSection及Html.RenderPartial、Html.RenderAction
    @html.ActionLink的几种参数格式
    MVC session过期如何处理跳转
    LINQ to SQL和Entity Framework对比与关联
    ASP.Net 更新页面输出缓存的几种方法
    asp.net缓存
    ASP.NET应用程序与页面生命周期
    两种图片延迟加载的方法总结jquery.scrollLoading.js与jquery.lazyload.js
    测试入门知识
  • 原文地址:https://www.cnblogs.com/mibing/p/7661490.html
Copyright © 2011-2022 走看看