使用HttpModule(给页面添加页头和页尾,重写URL)
2013-03-11 20:05
477 查看
使用HttpModule(给页面添加页头和页尾)
Module.cs
#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
URL重写
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("/httpmodule/goumai/"))
{
//找到路径中最后一个"/"的位置
int start = requestPath.LastIndexOf("/");
//找到最后一个"."的位置
int end = requestPath.LastIndexOf(".");
string id = requestPath.Substring(start + 1, (end - start));
context.RewritePath("~/goumai/goumai.aspx?id=" + id);
}
}
#endregion
}
Web.config
<configuration>
<system.web>
<httpModules>
<add name="Text" type="Module"/>
<!--<add name="UrlRewriter" type="UrlRewriter"/>-->
</httpModules>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
</configuration>
Module.cs
#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
URL重写
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("/httpmodule/goumai/"))
{
//找到路径中最后一个"/"的位置
int start = requestPath.LastIndexOf("/");
//找到最后一个"."的位置
int end = requestPath.LastIndexOf(".");
string id = requestPath.Substring(start + 1, (end - start));
context.RewritePath("~/goumai/goumai.aspx?id=" + id);
}
}
#endregion
}
Web.config
<configuration>
<system.web>
<httpModules>
<add name="Text" type="Module"/>
<!--<add name="UrlRewriter" type="UrlRewriter"/>-->
</httpModules>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
</configuration>
相关文章推荐
- ASP.NET:使用HttpModule(给页面添加页头和页尾,重写URL)
- ASP.NET:使用HttpModule(给页面添加页头和页尾,重写URL)
- URL重写之在IIS7中使用HttpModule,在IIS5和IIS6中使用 ISAPIRewrite实现无扩展名的URL重写(节选自福娃儿)
- 使用 HttpModule 执行 URL 重写的时机选择
- 使用 HttpModule 执行 URL 重写的时机选择
- 在ASP.NET中重写URL 方法二:使用HttpModule实现URL重写
- 使用HttpModule实现网址重写和HttpHandler实现页面静态化冲突的解决办法
- 在ASP.NET中重写URL 方法三:在IIS7中使用HttpModule 实现无扩展名的URL重写
- Url地址重写,利用HttpHander手工编译页面并按需生成静态HTML文件
- 地址重写中使用httpModule映射.htm的问题
- Url地址重写,利用HttpHander手工编译页面并按需生成静态HTML文件
- Url地址重写,利用HttpHander手工编译页面并按需生成静态HTML文件
- 关于java response的两种页面输出方式,以及HttpUrlconnection 代理使用注意点
- apache url重写模块rewrite_module使用方法
- 地址重写中使用httpModule映射.htm的问题
- 利用自定义IHttpModule来实现URL地址重写
- asp.net 使用HttpModule对全站输出的动态页面的HTML内容进行修改,不会错乱
- 使用HttpURLConnection访问web页面
- 使用HttpURLConnection访问web页面