ASP.Net中自定义Http处理及应用之HttpModule篇
HttpHandler实现了类似于ISAPI Extention的功能,他处理请求(Request)的信息和发送响应(Response)。HttpHandler功能的实现通过实现IHttpHandler接口来达到。而HttpModule实现了类似于ISAPI Filter的功能。
HttpModule的实现
HttpModules实现了类似于ISAPI Filter的功能,在开发上,通常需要经过以下步骤:
编写一个类,实现IhttpModule接口
实现Init 方法,并且注册需要的方法
实现注册的方法
实现Dispose方法,如果需要手工为类做一些清除工作,可以添加Dispose方法的实现,但这不是必需的,通常可以不为Dispose方法添加任何代码。
在Web.config文件中,注册您编写的类
下面是一个HttpModules的示例,在这个示例中,只是简单的注册了HttpApplication 的BeginRequest 和 EndRequest事件,并且通过这些事件的实现方法,将相关的信息打印出来。
例1:
using System;
using System.Web;
namespace MyModule{
public class MyModule : IHttpModule {
public void Init(HttpApplication application) {
application.BeginRequest += (new
EventHandler(this.Application_BeginRequest));
application.EndRequest += (new
EventHandler(this.Application_EndRequest));
}
private void Application_BeginRequest(Object source, EventArgs e) {
HttpApplication Application = (HttpApplication)source;
HttpResponse Response=Application.Context.Response;
Response.Write("<h1>Beginning of Request</h1><hr>");
}
private void Application_EndRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpResponse Response=Application.Context.Response;
Response.Write("<h1>End of Request</h1><hr>");
}
public void Dispose() { }
}
}
程序的开始引用了如下名称空间:
using System;
using System.Web;
因为HttpApplication、HttpContext、HttpResponse等类在System.Web中定义,因此,System.Web名称空间是必须引用的。
MyModule类实现了IhttpModule接口。在Init方法中,指明了实现BeginRequest 和EndRequest 事件的方法。在这两个方法中,只是简单的分别打印了一些信息。
下面,在Web.config文件中注册这个类,就可以使用这个HttpModule了,注册的方法如下:
<configuration>
<system.web>
相关文档:
数据绑定控件比较(Reapter\DataList\GridView\DatailsView\FormView):
1.插入功能方面:
DetailsView和FormView具有插入功能,其它控件没有
2.模板
DataList\FormView\Repeater三种必须编辑模板,而
GridView和DetailsView只有在将列转换成模板列以后才会出现各种模板.
3.自动分页功能
GridView ,DetailsView和FormView ......
1. 打开新的窗口并传送参数:
传送参数:
response.write("<script>window.open(’*.aspx?id="+this.DropDownList1.SelectIndex+"&id1="+...+"’)</script>")
接收参数:
string a = Request.QueryString("id");
string b = Request.QueryString("id1");
2.为按钮添加对话框
Button1 ......
1、双击一个数据源控件,将要存储profile的数据库作为数据源加入控件(这个步骤没什么用,只是为了方便大家用控件建立连接,这样不容易出错),建立好之后将design页面的数据源控件删除,你会发现在web.config里还是有一条连接语句,不要删除,我们下面将用到它,如: <connectionStrings>
<add ......
模板化的数据绑定控件为我们在页面上显示数据提供了根本的灵活性。你可能还记得ASP.NET v1.x中的几个模板化控件(例如DataList和Repeater控件)。ASP.NET 2.0仍然支持这些控件,但在模板中绑定数据的语法已经被简化和改善了。本文将讨论在数据绑定控件模板中绑定数据的多种方法。
数据绑定表达式
ASP.NET 2.0改善了模板中 ......
@Register : Register a user control or class with alias to this page.
@Import: Import a namespace.
@Reference: Link user controls or other page to complile current page. 支持数据跨页面的传送
页面事件:PreInit(创建服务器控件), Init(初始化服务器控件的状态), InitComplete,PreLoad, Load, Lo ......