转:ASP.NET底层机制(下)
来源:互联网 发布:在淘宝上怎么兼职手模 编辑:程序博客网 时间:2024/06/04 17:56
1.IHttpHandler接口
定义了实现一个HttpRequest的处理所必须实现的一些系统约定方法。
public interface IHttpHandler
{
//其他Request是否可以使用IHttpHandler
bool IsReusable { get; }
//处理HttpRequest
void ProcessRequest(HttpContext context);
}
{
//其他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"]);
}
}
}
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>
<add verb="*" path="*" type="MyNamespace.MyHandler,MyNamespace"></add>
</httpHandlers>
4.IHttpHandlerFactory
待续。。。
- 转:ASP.NET底层机制(下)
- ASP.NET底层机制 (下) HttpHandler
- ASP.NET底层机制 (下) HttpHandler 【转载】
- Asp.net底层机制
- Asp.Net底层机制
- 转:ASP.NET底层机制 (上) HTTPModule
- ASP.NET底层机制 (上) HttpModule
- ASP.NET底层机制 (上) HttpModule[转载]
- ASP.NET底层机制 ashx页面
- Asp.Net底层解析(一)——常规ASPX页面的实现机制
- ASP.NET WebForms底层请求处理机制初探
- ASP.NET WebForms底层请求处理机制初探
- ASP.NET WebForms底层请求处理机制初探
- ASP.NET的底层的工作机制介绍
- ASP.NET的底层的工作机制介绍
- Asp.net底层探秘之Http请求机制
- ASP.NET WebForms底层请求处理机制初探
- ASP.NET的底层的工作机制介绍
- 转:ASP.NET底层机制 (上) HTTPModule
- weblogic10.3 异常:org.hibernate.hql.ast.HqlToken
- RegExp 对象(二)
- 肖申克的救赎
- 三星uboot1.1.6源码分析——start.s(2)--从NAND复制uboot源码到RAM(1)
- 转:ASP.NET底层机制(下)
- static类成员
- Linux设备驱动学习之 Poll机制分析
- 博客相关
- HBase本地环境的配置与搭建
- Android的Message机制(简单小结)
- ios 6 beta试用记录
- UITextField可以重设输入框内部的显示区域
- Spring基础(二)之手动装配JavaBean