ASP.NET:使用HttpModule(给页面添加页头和页尾,重写URL)
2013-02-20 12:56
441 查看
![](http://images.cnblogs.com/cnblogs_com/ylbtech/403118/o_ylbtech-httpmodule.jpg)
1.1.2,使用HttpModule(给页面添加页头和页尾) 返回顶部 /App_Code/SimpleModule.cs 复制代码 using System; using System.Collections.Generic; using System.Linq; using System.Web; //<system.web> // <httpModules> // <add name="Text" type="SimpleModule"/> // <!--<add name="UrlRewriter" type="UrlRewriter"/>--> // </httpModules> //</system.web> /// <summary> ///SimpeModule 的摘要说明 /// </summary> public class SimpleModule:IHttpModule { public SimpleModule() { // //TODO: 在此处添加构造函数逻辑 // } #region IHttpModule 成员 public void Dispose() { } public void Init(HttpApplication context) { context.BeginRequest +=new EventHandler(context_BeginRequest); context.EndRequest +=new EventHandler(context_EndRequest); } void context_EndRequest(object sender, EventArgs e) { ((HttpApplication)sender).Response.Output.Write("<hr>页尾"); } void context_BeginRequest(object sender, EventArgs e) { ((HttpApplication)sender).Response.Output.Write("页头<hr>"); } #endregion } 复制代码 1.1.3,URL重写 返回顶部 App_Code/UrlWrite.cs 复制代码 using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> ///UrlRewriter 的摘要说明 /// </summary> public class UrlRewriter:IHttpModule { public UrlRewriter() { // //TODO: 在此处添加构造函数逻辑 // } #region IHttpModule 成员 public void Dispose() { } public void Init(HttpApplication context) { context.BeginRequest += new EventHandler(context_BeginRequest); ; } void context_BeginRequest(object sender, EventArgs e) { HttpContext context = ((HttpApplication)sender).Context; string requestPath = context.Request.Path.ToLower(); //判断请求路径是否为鲜花页面 if (requestPath.Contains("/webhttpmodule/flower/")) { //找到路径中最后一个"/"的位置 int start = requestPath.LastIndexOf("/"); //找到最后一个"."的位置 int end = requestPath.LastIndexOf("."); string id = requestPath.Substring(start + 1, (end - start)); context.RewritePath("~/flower/flower.aspx?id=" + id); } } #endregion } 复制代码 web.config 复制代码 <?xml version="1.0"?> <!-- 有关如何配置 ASP.NET 应用程序的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <system.web> <httpModules> <add name="Text" type="SimpleModule"/> <!--<add name="UrlRewriter" type="UrlRewriter"/>--> </httpModules> <compilation debug="true" targetFramework="4.0"/> </system.web> </configuration> 复制代码
相关文章推荐
- ASP.NET:使用HttpModule(给页面添加页头和页尾,重写URL)
- 使用HttpModule(给页面添加页头和页尾,重写URL)
- 在ASP.NET中重写URL 方法二:使用HttpModule实现URL重写
- 在ASP.NET中重写URL 方法三:在IIS7中使用HttpModule 实现无扩展名的URL重写
- [转]asp.net通过HttpModule自动在Url地址上添加参数
- asp.net 使用HttpModule对全站输出的动态页面的HTML内容进行修改,不会错乱
- asp.net通过HttpModule自动在Url地址上添加参数
- 在ASP.NET中重写URL 方法一:使用Request.PathInfo 参数而不是查询字符串
- 一:理解ASP.NET的运行机制(例:通过HttpModule来计算页面执行时间)
- asp.net mvc web url 伪静态页面重写技术
- ASP.NET页面执行顺序 (HttpModule,HttpHandler)
- 使用js 文件参数 以及IHttpModule实现服务验证asp.net 版的初步实现
- asp.net 使用 httpModules 实现伪URL地址
- 截获asp.net上传文件过大IIS报错的Httpmodule代码 --方便以后使用
- 加快页面加载速度的解决方案-asp.net使用httphandler打包多CSS或JS文件
- Asp.net使用HttpModule压缩并删除空白Html请求
- ASP.NET中Url重写后,打不开真正的Html页面
- c#asp.net url 传递中文参数要使用 System.Web.HttpUtility.UrlEncode 而不能使用Server.UrlEncode
- ASP.NET MVC 中使用 HttpModule 来响应或动态改变静态文件