ASP.NET:使用HttpModule(给页面添加页头和页尾,重写URL)

ylbtech-asp.net:内容简介
技术与环境
操作系统:windows语言类别:C#
thankyou:sunshine, 谢谢你的默默付出课题:HttpModule
学习软件:Microsoft Visaul Studio 2010
1.1.1,Init方法注册事件返回顶部

 

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>
复制代码

 

本文转自ylbtech博客园博客,原文链接:http://www.cnblogs.com/ylbtech/archive/2013/02/16/2878610.html,如需转载请自行联系原作者


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值