ASP.NET学习笔记(四)-模板页,HttpHandler,IHttpModule应用
来源:互联网 发布:文明网络行教学反思 编辑:程序博客网 时间:2024/05/16 09:55
6.模版页 (1).加入模版页可以是将图片粘贴到网站,最好新建一个文件夹,这样看起来分类较细 (2).在网站新建项里建立一个建立一个模版页,即.master,加入样式(主标题在div外面写字,可用左右键切换,添加一个Image控件,将ImageUrl选为内连的图片,最好样式的长宽住设计视图里搞好,不然就得用相应的 td的 width ="15%" bordergrouder-color="Grey" 或:style=" width:100px; bordergrouder-color:50%" 来改,) (3).修改本来继承其他网页模版的代码: protected void Page_PreInit(object sender, EventArgs e) { this.MasterPageFile = "~/MasterPage2.master"; }7.HttpHandler的使用 (1). 在ProcessRequest 事件中写反馈页面 (2).在Config的System.web下的Httphandler 下add自己的 verb, path写被访问的后缀名 (随便写), type 是(写为绝对路径) 具体代码如下: <add verb="*" path="*.sample" type="MyHttpHandler"/> //这里的type没有引入命名空间,所以就不存在 完整路径,直接是类名 using System; using System.Collections.Generic; using System.Linq; using System.Web;/// <summary>///MyHttpHandler 的摘要说明/// </summary>public class MyHttpHandler:IHttpHandler //记住是IHttpHandler ,不是IHttpAnyHandler。。。。。。{ public MyHttpHandler() { // //TODO: 在此处添加构造函数逻辑 // } public bool IsReusable { get { return false; } } public void ProcessRequest(HttpContext context) //这是一个返回请求的页面,依然用HttpResponse 的实例去Write网页.......... { //对于错误报告等返回很有效,也可以作为Session的验证。。。不反馈前台............. //也可以返回与前台页面相同的页面,不过要write包的麻烦些 记住是在这个事件里写 HttpResponse response = context.Response; response.Write("<html>"); response.Write("<head>"); response.Write("<title> 这是来MyHandler的相应</title>"); response.Write("</head>"); response.Write("<body>"); response.Write("<h1 style =\"color:red\"> 这是来MyHandler的相应</h1>"); response.Write("</body"); response.Write("</html>"); }} 在Web.config中: <add verb="*" path="*.sample" type="MyHttpHandler"/> 8.Http模块的IHttpModule类的使用(类模块,类似与模版类模版) (1).一般的方法是继承的IHttpModule,里面有Begin 的require 即在客户端的Http请求具体页面发过来的之前,就先请求该模块的事件,类似的类方法还有End require,即在最后的请求中才请求该模块的信息(一般放的是页脚的版权信息,每页必有) (2).该类的事件都是在HTML的页面之外的情况... (3).在Inti里写初始化,连续两次按tab键,出方法using System;using System.Collections.Generic;using System.Linq;using System.Web;/// <summary>///MyHttpMudule 的摘要说明/// </summary>public class MyHttpMudule:IHttpModule{ public MyHttpMudule() { // //TODO: 在此处添加构造函数逻辑 // } public void Dispose() { return; } public void Init(HttpApplication context) { context.BeginRequest += new EventHandler(context_BeginRequest); //连续按两次Tab键出下面的方法 context.EndRequest += new EventHandler(context_EndRequest); context.AcquireRequestState += new EventHandler(context_AcquireRequestState); } /* void context_AcquireRequestState(object sender, EventArgs e) { HttpApplication application = sender as HttpApplication; System.Web.SessionState.HttpSessionState session = application.Session; //获取Session if (null == session["username"]) { HttpResponse response = application.Response; response.Redirect("~/UserLogin.aspx"); response.Redirect("~/UserLogin.aspx/"); //导向到新的页面去,用response.Redirect 重定向 但是这里会造成死循环,因为每个页面都会重复进行Session的验证,由于Session里为空,所以会跳回自己 response.Redirect("~/UserLogin.aspx"); application.Server.Transfer("~/UserLogin.aspx");//解决的办法代替 response.Redirect("~/UserLogin.aspx"); //这里直接请求UserLogin.aspx,而不经过Session模块 }*/ //或者在之前判断一下是否是UserLogin.aspx页面,如果是,就不需判断,直接返回 void context_AcquireRequestState(object sender, EventArgs e) { HttpApplication application = sender as HttpApplication; string filepath = application.Request.FilePath;//返回请求的文件路径 string FileName = VirtualPathUtility.GetFileName(filepath);//返回文件的最后的完整文件名 if (FileName == "UserLogin.aspx") return; //导向到新的页面去,用response.Redirect 重定向,跳转到新的页面 System.Web.SessionState.HttpSessionState session = application.Session; //获取Session if (null == session["username"]) { HttpResponse response = application.Response; response.Redirect("~/UserLogin.aspx"); /* response.Redirect("~/UserLogin.aspx/"); //导向到新的页面去,用response.Redirect 重定向 但是这里会造成死循环,因为每个页面都会重复进行Session的验证,由于Session里为空,所以会跳回自己 response.Redirect("~/UserLogin.aspx"); application.Server.Transfer("~/UserLogin.aspx");//解决的办法代替 response.Redirect("~/UserLogin.aspx"); * 在后缀名不变的情况下直接显示转移网页的内容,该方法不会再次模拟提交的客户端,所以不会再次经过Http模块,该模块可以随时的进行加删,所以很方便,这是单点登录的很好技巧 */ //或者在之前判断一下是否是UserLogin.aspx页面,如果是,就不需判断,直接返回 } } void context_EndRequest(object sender, EventArgs e) { HttpApplication application = sender as HttpApplication; HttpResponse response = application.Response; response.Write("<h1 style =\"color: blue\">这是一个来自HTTP模块的版权信息</h1>");//通过源码可以看出,是在html的题尾 } void context_BeginRequest(object sender, EventArgs e)//这是在未请求default 页面之前进行的,即所有页面都会有的东西,但是要在config中的httphandler里注册,所以可以使用这个作为session。。。。。。全局网站验证 { HttpApplication applicition = sender as HttpApplication; HttpResponse response = applicition.Response; response.Write("<h1 style =\"color: red\">这是一个来自HTTP模块的题头</h1>");//通过源码可以看出,是在所有html最前面 }}
0 0
- ASP.NET学习笔记(四)-模板页,HttpHandler,IHttpModule应用
- ASP.NET学习笔记(四)续
- ASP.NET学习笔记(四)
- ASP.NET基础之HttpHandler学习
- ASP.NET基础之HttpHandler学习
- 菜鸟学习IHttpModule 接口笔记(一)
- asp.net HttpModule与IHttpModule
- ASP.NET温故而知新学习系列之深度剖析ASP.NET架构—HttpHandler(三)
- ASP.NET底层机制 (下) HttpHandler
- ASP.NET (HttpModule,HttpHandler)
- ASP.NET HttpHandler简介
- Asp.Net中的HttpHandler
- ASP.NET 中的 HttpHandler
- ASP.NET (HttpModule,HttpHandler)
- ASP.NET基础 HttpHandler
- IHttpModule和IHttpHandler 应用笔记
- IHttpModule和IHttpHandler 应用笔记
- ASP.Net 3.5学习笔记(C#)四
- Shell脚本调试技术
- 非关系型数据库——Mongodb
- 利用JavaScript检测CPU使用率
- ASP.NET学习笔记(三)-可空数据类型,强类型DataSet
- 黑马程序员_关于虚拟机内存的知识点补充
- ASP.NET学习笔记(四)-模板页,HttpHandler,IHttpModule应用
- Building X10 from Source
- 钟表上的数学问题
- MongoDB——增删查改
- 听说是大数阶乘,没测过!!
- Inside the c++ object model读书笔记之拷贝构造函数(二)
- ASP.NET学习笔记(五)-全球化部署,网站发布方法,AJAX使用,水晶报表使用,DropDownList,CheckBox全选
- tlplayer,wzplayer支持wince,winphone,windows8 for arm
- Android 用剪切板传递数据